blob: 6cbe78db8774f476000120014a3f693ec9988dd1 [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 Salyzyn7cb0e732014-04-18 13:48:25 -070019#include <log/log.h>
20#include <inttypes.h>
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080021#include <stdlib.h>
22#include <string.h>
23#include <stdbool.h>
24#include "EffectDownmix.h"
25
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -070026// Do not submit with DOWNMIX_TEST_CHANNEL_INDEX defined, strictly for testing
27//#define DOWNMIX_TEST_CHANNEL_INDEX 0
28// Do not submit with DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER defined, strictly for testing
29//#define DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER 0
30
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080031#define MINUS_3_DB_IN_Q19_12 2896 // -3dB = 0.707 * 2^12 = 2896
32
Glenn Kasten029a64e2014-04-15 09:58:55 -070033// subset of possible audio_channel_mask_t values, and AUDIO_CHANNEL_OUT_* renamed to CHANNEL_MASK_*
Jean-Michel Trividb46b482012-04-23 11:29:26 -070034typedef enum {
Glenn Kasten029a64e2014-04-15 09:58:55 -070035 CHANNEL_MASK_QUAD_BACK = AUDIO_CHANNEL_OUT_QUAD_BACK,
36 CHANNEL_MASK_QUAD_SIDE = AUDIO_CHANNEL_OUT_QUAD_SIDE,
37 CHANNEL_MASK_5POINT1_BACK = AUDIO_CHANNEL_OUT_5POINT1_BACK,
38 CHANNEL_MASK_5POINT1_SIDE = AUDIO_CHANNEL_OUT_5POINT1_SIDE,
39 CHANNEL_MASK_7POINT1 = AUDIO_CHANNEL_OUT_7POINT1,
Jean-Michel Trividb46b482012-04-23 11:29:26 -070040} downmix_input_channel_mask_t;
41
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080042// effect_handle_t interface implementation for downmix effect
43const struct effect_interface_s gDownmixInterface = {
44 Downmix_Process,
45 Downmix_Command,
46 Downmix_GetDescriptor,
47 NULL /* no process_reverse function, no reference stream needed */
48};
49
Marco Nelissen7f16b192012-10-25 16:05:57 -070050// This is the only symbol that needs to be exported
51__attribute__ ((visibility ("default")))
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080052audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
synergydevc9d8ea72013-10-19 22:51:33 -070053 .tag = AUDIO_EFFECT_LIBRARY_TAG,
54 .version = EFFECT_LIBRARY_API_VERSION,
55 .name = "Downmix Library",
56 .implementor = "The Android Open Source Project",
57 .create_effect = DownmixLib_Create,
58 .release_effect = DownmixLib_Release,
59 .get_descriptor = DownmixLib_GetDescriptor,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -080060};
61
62
63// AOSP insert downmix UUID: 93f04452-e4fe-41cc-91f9-e475b6d1d69f
64static const effect_descriptor_t gDownmixDescriptor = {
65 EFFECT_UIID_DOWNMIX__, //type
66 {0x93f04452, 0xe4fe, 0x41cc, 0x91f9, {0xe4, 0x75, 0xb6, 0xd1, 0xd6, 0x9f}}, // uuid
67 EFFECT_CONTROL_API_VERSION,
68 EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
69 0, //FIXME what value should be reported? // cpu load
70 0, //FIXME what value should be reported? // memory usage
71 "Multichannel Downmix To Stereo", // human readable effect name
72 "The Android Open Source Project" // human readable effect implementor name
73};
74
75// gDescriptors contains pointers to all defined effect descriptor in this library
76static const effect_descriptor_t * const gDescriptors[] = {
77 &gDownmixDescriptor
78};
79
80// number of effects in this library
81const int kNbEffects = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
82
83
84/*----------------------------------------------------------------------------
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -070085 * Test code
86 *--------------------------------------------------------------------------*/
87#ifdef DOWNMIX_TEST_CHANNEL_INDEX
88// strictly for testing, logs the indices of the channels for a given mask,
89// uses the same code as Downmix_foldGeneric()
90void Downmix_testIndexComputation(uint32_t mask) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -070091 ALOGI("Testing index computation for 0x%" PRIx32 ":", mask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -070092 // check against unsupported channels
93 if (mask & kUnsupported) {
94 ALOGE("Unsupported channels (top or front left/right of center)");
95 return;
96 }
97 // verify has FL/FR
98 if ((mask & AUDIO_CHANNEL_OUT_STEREO) != AUDIO_CHANNEL_OUT_STEREO) {
99 ALOGE("Front channels must be present");
100 return;
101 }
102 // verify uses SIDE as a pair (ok if not using SIDE at all)
103 bool hasSides = false;
104 if ((mask & kSides) != 0) {
105 if ((mask & kSides) != kSides) {
106 ALOGE("Side channels must be used as a pair");
107 return;
108 }
109 hasSides = true;
110 }
111 // verify uses BACK as a pair (ok if not using BACK at all)
112 bool hasBacks = false;
113 if ((mask & kBacks) != 0) {
114 if ((mask & kBacks) != kBacks) {
115 ALOGE("Back channels must be used as a pair");
116 return;
117 }
118 hasBacks = true;
119 }
120
Andy Hunge5412692014-05-16 11:25:07 -0700121 const int numChan = audio_channel_count_from_out_mask(mask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700122 const bool hasFC = ((mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) == AUDIO_CHANNEL_OUT_FRONT_CENTER);
123 const bool hasLFE =
124 ((mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) == AUDIO_CHANNEL_OUT_LOW_FREQUENCY);
125 const bool hasBC = ((mask & AUDIO_CHANNEL_OUT_BACK_CENTER) == AUDIO_CHANNEL_OUT_BACK_CENTER);
126 // compute at what index each channel is: samples will be in the following order:
127 // FL FR FC LFE BL BR BC SL SR
128 // when a channel is not present, its index is set to the same as the index of the preceding
129 // channel
130 const int indexFC = hasFC ? 2 : 1; // front center
131 const int indexLFE = hasLFE ? indexFC + 1 : indexFC; // low frequency
132 const int indexBL = hasBacks ? indexLFE + 1 : indexLFE; // back left
133 const int indexBR = hasBacks ? indexBL + 1 : indexBL; // back right
134 const int indexBC = hasBC ? indexBR + 1 : indexBR; // back center
135 const int indexSL = hasSides ? indexBC + 1 : indexBC; // side left
136 const int indexSR = hasSides ? indexSL + 1 : indexSL; // side right
137
138 ALOGI(" FL FR FC LFE BL BR BC SL SR");
139 ALOGI(" %d %d %d %d %d %d %d %d %d",
140 0, 1, indexFC, indexLFE, indexBL, indexBR, indexBC, indexSL, indexSR);
141}
142#endif
143
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -0700144static bool Downmix_validChannelMask(uint32_t mask)
145{
146 if (!mask) {
147 return false;
148 }
149 // check against unsupported channels
150 if (mask & kUnsupported) {
151 ALOGE("Unsupported channels (top or front left/right of center)");
152 return false;
153 }
154 // verify has FL/FR
155 if ((mask & AUDIO_CHANNEL_OUT_STEREO) != AUDIO_CHANNEL_OUT_STEREO) {
156 ALOGE("Front channels must be present");
157 return false;
158 }
159 // verify uses SIDE as a pair (ok if not using SIDE at all)
160 if ((mask & kSides) != 0) {
161 if ((mask & kSides) != kSides) {
162 ALOGE("Side channels must be used as a pair");
163 return false;
164 }
165 }
166 // verify uses BACK as a pair (ok if not using BACK at all)
167 if ((mask & kBacks) != 0) {
168 if ((mask & kBacks) != kBacks) {
169 ALOGE("Back channels must be used as a pair");
170 return false;
171 }
172 }
173 return true;
174}
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700175
176/*----------------------------------------------------------------------------
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800177 * Effect API implementation
178 *--------------------------------------------------------------------------*/
179
180/*--- Effect Library Interface Implementation ---*/
181
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800182int32_t DownmixLib_Create(const effect_uuid_t *uuid,
Eric Laurent0f714a42015-06-19 15:33:57 -0700183 int32_t sessionId __unused,
184 int32_t ioId __unused,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800185 effect_handle_t *pHandle) {
186 int ret;
187 int i;
188 downmix_module_t *module;
189 const effect_descriptor_t *desc;
190
191 ALOGV("DownmixLib_Create()");
192
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700193#ifdef DOWNMIX_TEST_CHANNEL_INDEX
194 // should work (won't log an error)
195 ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should work:");
196 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
197 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_CENTER);
198 Downmix_testIndexComputation(CHANNEL_MASK_QUAD_SIDE | CHANNEL_MASK_QUAD_BACK);
199 Downmix_testIndexComputation(CHANNEL_MASK_5POINT1_SIDE | AUDIO_CHANNEL_OUT_BACK_CENTER);
200 Downmix_testIndexComputation(CHANNEL_MASK_5POINT1_BACK | AUDIO_CHANNEL_OUT_BACK_CENTER);
201 // shouldn't work (will log an error, won't display channel indices)
202 ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should NOT work:");
203 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
204 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_LEFT);
205 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
206 AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_SIDE_LEFT);
207 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
208 AUDIO_CHANNEL_OUT_BACK_LEFT | AUDIO_CHANNEL_OUT_BACK_RIGHT);
209 Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
210 AUDIO_CHANNEL_OUT_SIDE_LEFT | AUDIO_CHANNEL_OUT_SIDE_RIGHT);
211#endif
212
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800213 if (pHandle == NULL || uuid == NULL) {
214 return -EINVAL;
215 }
216
217 for (i = 0 ; i < kNbEffects ; i++) {
218 desc = gDescriptors[i];
219 if (memcmp(uuid, &desc->uuid, sizeof(effect_uuid_t)) == 0) {
220 break;
221 }
222 }
223
224 if (i == kNbEffects) {
225 return -ENOENT;
226 }
227
228 module = malloc(sizeof(downmix_module_t));
229
230 module->itfe = &gDownmixInterface;
231
232 module->context.state = DOWNMIX_STATE_UNINITIALIZED;
233
234 ret = Downmix_Init(module);
235 if (ret < 0) {
236 ALOGW("DownmixLib_Create() init failed");
237 free(module);
238 return ret;
239 }
240
241 *pHandle = (effect_handle_t) module;
242
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700243 ALOGV("DownmixLib_Create() %p , size %zu", module, sizeof(downmix_module_t));
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800244
245 return 0;
246}
247
248
249int32_t DownmixLib_Release(effect_handle_t handle) {
250 downmix_module_t *pDwmModule = (downmix_module_t *)handle;
251
252 ALOGV("DownmixLib_Release() %p", handle);
253 if (handle == NULL) {
254 return -EINVAL;
255 }
256
257 pDwmModule->context.state = DOWNMIX_STATE_UNINITIALIZED;
258
259 free(pDwmModule);
260 return 0;
261}
262
263
264int32_t DownmixLib_GetDescriptor(const effect_uuid_t *uuid, effect_descriptor_t *pDescriptor) {
265 ALOGV("DownmixLib_GetDescriptor()");
266 int i;
267
268 if (pDescriptor == NULL || uuid == NULL){
269 ALOGE("DownmixLib_Create() called with NULL pointer");
270 return -EINVAL;
271 }
272 ALOGV("DownmixLib_GetDescriptor() nb effects=%d", kNbEffects);
273 for (i = 0; i < kNbEffects; i++) {
274 ALOGV("DownmixLib_GetDescriptor() i=%d", i);
275 if (memcmp(uuid, &gDescriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
276 memcpy(pDescriptor, gDescriptors[i], sizeof(effect_descriptor_t));
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700277 ALOGV("EffectGetDescriptor - UUID matched downmix type %d, UUID = %" PRIx32,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800278 i, gDescriptors[i]->uuid.timeLow);
279 return 0;
280 }
281 }
282
283 return -EINVAL;
284}
285
286
287/*--- Effect Control Interface Implementation ---*/
288
289static int Downmix_Process(effect_handle_t self,
290 audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) {
291
292 downmix_object_t *pDownmixer;
293 int16_t *pSrc, *pDst;
294 downmix_module_t *pDwmModule = (downmix_module_t *)self;
295
296 if (pDwmModule == NULL) {
297 return -EINVAL;
298 }
299
300 if (inBuffer == NULL || inBuffer->raw == NULL ||
301 outBuffer == NULL || outBuffer->raw == NULL ||
302 inBuffer->frameCount != outBuffer->frameCount) {
303 return -EINVAL;
304 }
305
306 pDownmixer = (downmix_object_t*) &pDwmModule->context;
307
308 if (pDownmixer->state == DOWNMIX_STATE_UNINITIALIZED) {
309 ALOGE("Downmix_Process error: trying to use an uninitialized downmixer");
310 return -EINVAL;
311 } else if (pDownmixer->state == DOWNMIX_STATE_INITIALIZED) {
312 ALOGE("Downmix_Process error: trying to use a non-configured downmixer");
313 return -ENODATA;
314 }
315
316 pSrc = inBuffer->s16;
317 pDst = outBuffer->s16;
318 size_t numFrames = outBuffer->frameCount;
319
320 const bool accumulate =
321 (pDwmModule->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700322 const uint32_t downmixInputChannelMask = pDwmModule->config.inputCfg.channels;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800323
324 switch(pDownmixer->type) {
325
326 case DOWNMIX_TYPE_STRIP:
327 if (accumulate) {
328 while (numFrames) {
329 pDst[0] = clamp16(pDst[0] + pSrc[0]);
330 pDst[1] = clamp16(pDst[1] + pSrc[1]);
331 pSrc += pDownmixer->input_channel_count;
332 pDst += 2;
333 numFrames--;
334 }
335 } else {
336 while (numFrames) {
337 pDst[0] = pSrc[0];
338 pDst[1] = pSrc[1];
339 pSrc += pDownmixer->input_channel_count;
340 pDst += 2;
341 numFrames--;
342 }
343 }
344 break;
345
346 case DOWNMIX_TYPE_FOLD:
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700347#ifdef DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER
348 // bypass the optimized downmix routines for the common formats
349 if (!Downmix_foldGeneric(
350 downmixInputChannelMask, pSrc, pDst, numFrames, accumulate)) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700351 ALOGE("Multichannel configuration 0x%" PRIx32 " is not supported", downmixInputChannelMask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700352 return -EINVAL;
353 }
354 break;
355#endif
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800356 // optimize for the common formats
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700357 switch((downmix_input_channel_mask_t)downmixInputChannelMask) {
Jean-Michel Trividb46b482012-04-23 11:29:26 -0700358 case CHANNEL_MASK_QUAD_BACK:
359 case CHANNEL_MASK_QUAD_SIDE:
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800360 Downmix_foldFromQuad(pSrc, pDst, numFrames, accumulate);
361 break;
Jean-Michel Trividb46b482012-04-23 11:29:26 -0700362 case CHANNEL_MASK_5POINT1_BACK:
363 case CHANNEL_MASK_5POINT1_SIDE:
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800364 Downmix_foldFrom5Point1(pSrc, pDst, numFrames, accumulate);
365 break;
Glenn Kasten029a64e2014-04-15 09:58:55 -0700366 case CHANNEL_MASK_7POINT1:
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800367 Downmix_foldFrom7Point1(pSrc, pDst, numFrames, accumulate);
368 break;
369 default:
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700370 if (!Downmix_foldGeneric(
371 downmixInputChannelMask, pSrc, pDst, numFrames, accumulate)) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700372 ALOGE("Multichannel configuration 0x%" PRIx32 " is not supported", downmixInputChannelMask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700373 return -EINVAL;
374 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800375 break;
376 }
377 break;
378
379 default:
380 return -EINVAL;
381 }
382
383 return 0;
384}
385
386
387static int Downmix_Command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
388 void *pCmdData, uint32_t *replySize, void *pReplyData) {
389
390 downmix_module_t *pDwmModule = (downmix_module_t *) self;
391 downmix_object_t *pDownmixer;
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800392
393 if (pDwmModule == NULL || pDwmModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
394 return -EINVAL;
395 }
396
397 pDownmixer = (downmix_object_t*) &pDwmModule->context;
398
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700399 ALOGV("Downmix_Command command %" PRIu32 " cmdSize %" PRIu32, cmdCode, cmdSize);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800400
401 switch (cmdCode) {
402 case EFFECT_CMD_INIT:
Eric Laurent0f714a42015-06-19 15:33:57 -0700403 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800404 return -EINVAL;
405 }
406 *(int *) pReplyData = Downmix_Init(pDwmModule);
407 break;
408
409 case EFFECT_CMD_SET_CONFIG:
410 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
Eric Laurent0f714a42015-06-19 15:33:57 -0700411 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800412 return -EINVAL;
413 }
414 *(int *) pReplyData = Downmix_Configure(pDwmModule,
415 (effect_config_t *)pCmdData, false);
416 break;
417
418 case EFFECT_CMD_RESET:
419 Downmix_Reset(pDownmixer, false);
420 break;
421
422 case EFFECT_CMD_GET_PARAM:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700423 ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM pCmdData %p, *replySize %" PRIu32 ", pReplyData: %p",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800424 pCmdData, *replySize, pReplyData);
425 if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
Eric Laurent0f714a42015-06-19 15:33:57 -0700426 pReplyData == NULL || replySize == NULL ||
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800427 *replySize < (int) sizeof(effect_param_t) + 2 * sizeof(int32_t)) {
428 return -EINVAL;
429 }
430 effect_param_t *rep = (effect_param_t *) pReplyData;
431 memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(int32_t));
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700432 ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM param %" PRId32 ", replySize %" PRIu32,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800433 *(int32_t *)rep->data, rep->vsize);
434 rep->status = Downmix_getParameter(pDownmixer, *(int32_t *)rep->data, &rep->vsize,
435 rep->data + sizeof(int32_t));
436 *replySize = sizeof(effect_param_t) + sizeof(int32_t) + rep->vsize;
437 break;
438
439 case EFFECT_CMD_SET_PARAM:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700440 ALOGV("Downmix_Command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %" PRIu32
441 ", pReplyData %p", cmdSize, pCmdData, *replySize, pReplyData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800442 if (pCmdData == NULL || (cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)))
Eric Laurent0f714a42015-06-19 15:33:57 -0700443 || pReplyData == NULL || replySize == NULL || *replySize != (int)sizeof(int32_t)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800444 return -EINVAL;
445 }
446 effect_param_t *cmd = (effect_param_t *) pCmdData;
447 *(int *)pReplyData = Downmix_setParameter(pDownmixer, *(int32_t *)cmd->data,
448 cmd->vsize, cmd->data + sizeof(int32_t));
449 break;
450
451 case EFFECT_CMD_SET_PARAM_DEFERRED:
452 //FIXME implement
453 ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_DEFERRED not supported, FIXME");
454 break;
455
456 case EFFECT_CMD_SET_PARAM_COMMIT:
457 //FIXME implement
458 ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_COMMIT not supported, FIXME");
459 break;
460
461 case EFFECT_CMD_ENABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700462 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800463 return -EINVAL;
464 }
465 if (pDownmixer->state != DOWNMIX_STATE_INITIALIZED) {
466 return -ENOSYS;
467 }
468 pDownmixer->state = DOWNMIX_STATE_ACTIVE;
469 ALOGV("EFFECT_CMD_ENABLE() OK");
470 *(int *)pReplyData = 0;
471 break;
472
473 case EFFECT_CMD_DISABLE:
Eric Laurent0f714a42015-06-19 15:33:57 -0700474 if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800475 return -EINVAL;
476 }
477 if (pDownmixer->state != DOWNMIX_STATE_ACTIVE) {
478 return -ENOSYS;
479 }
480 pDownmixer->state = DOWNMIX_STATE_INITIALIZED;
481 ALOGV("EFFECT_CMD_DISABLE() OK");
482 *(int *)pReplyData = 0;
483 break;
484
485 case EFFECT_CMD_SET_DEVICE:
486 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
487 return -EINVAL;
488 }
489 // FIXME change type if playing on headset vs speaker
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700490 ALOGV("Downmix_Command EFFECT_CMD_SET_DEVICE: 0x%08" PRIx32, *(uint32_t *)pCmdData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800491 break;
492
493 case EFFECT_CMD_SET_VOLUME: {
494 // audio output is always stereo => 2 channel volumes
495 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t) * 2) {
496 return -EINVAL;
497 }
498 // FIXME change volume
499 ALOGW("Downmix_Command command EFFECT_CMD_SET_VOLUME not supported, FIXME");
500 float left = (float)(*(uint32_t *)pCmdData) / (1 << 24);
501 float right = (float)(*((uint32_t *)pCmdData + 1)) / (1 << 24);
502 ALOGV("Downmix_Command EFFECT_CMD_SET_VOLUME: left %f, right %f ", left, right);
503 break;
504 }
505
506 case EFFECT_CMD_SET_AUDIO_MODE:
507 if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
508 return -EINVAL;
509 }
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700510 ALOGV("Downmix_Command EFFECT_CMD_SET_AUDIO_MODE: %" PRIu32, *(uint32_t *)pCmdData);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800511 break;
512
513 case EFFECT_CMD_SET_CONFIG_REVERSE:
514 case EFFECT_CMD_SET_INPUT_DEVICE:
515 // these commands are ignored by a downmix effect
516 break;
517
518 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700519 ALOGW("Downmix_Command invalid command %" PRIu32, cmdCode);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800520 return -EINVAL;
521 }
522
523 return 0;
524}
525
526
527int Downmix_GetDescriptor(effect_handle_t self, effect_descriptor_t *pDescriptor)
528{
529 downmix_module_t *pDwnmxModule = (downmix_module_t *) self;
530
531 if (pDwnmxModule == NULL ||
532 pDwnmxModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
533 return -EINVAL;
534 }
535
536 memcpy(pDescriptor, &gDownmixDescriptor, sizeof(effect_descriptor_t));
537
538 return 0;
539}
540
541
542/*----------------------------------------------------------------------------
543 * Downmix internal functions
544 *--------------------------------------------------------------------------*/
545
546/*----------------------------------------------------------------------------
547 * Downmix_Init()
548 *----------------------------------------------------------------------------
549 * Purpose:
550 * Initialize downmix context and apply default parameters
551 *
552 * Inputs:
553 * pDwmModule pointer to downmix effect module
554 *
555 * Outputs:
556 *
557 * Returns:
558 * 0 indicates success
559 *
560 * Side Effects:
561 * updates:
562 * pDwmModule->context.type
563 * pDwmModule->context.apply_volume_correction
564 * pDwmModule->config.inputCfg
565 * pDwmModule->config.outputCfg
566 * pDwmModule->config.inputCfg.samplingRate
567 * pDwmModule->config.outputCfg.samplingRate
568 * pDwmModule->context.state
569 * doesn't set:
570 * pDwmModule->itfe
571 *
572 *----------------------------------------------------------------------------
573 */
574
575int Downmix_Init(downmix_module_t *pDwmModule) {
576
577 ALOGV("Downmix_Init module %p", pDwmModule);
578 int ret = 0;
579
580 memset(&pDwmModule->context, 0, sizeof(downmix_object_t));
581
582 pDwmModule->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
583 pDwmModule->config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
584 pDwmModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_7POINT1;
585 pDwmModule->config.inputCfg.bufferProvider.getBuffer = NULL;
586 pDwmModule->config.inputCfg.bufferProvider.releaseBuffer = NULL;
587 pDwmModule->config.inputCfg.bufferProvider.cookie = NULL;
588 pDwmModule->config.inputCfg.mask = EFFECT_CONFIG_ALL;
589
590 pDwmModule->config.inputCfg.samplingRate = 44100;
591 pDwmModule->config.outputCfg.samplingRate = pDwmModule->config.inputCfg.samplingRate;
592
593 // set a default value for the access mode, but should be overwritten by caller
594 pDwmModule->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
595 pDwmModule->config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
596 pDwmModule->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
597 pDwmModule->config.outputCfg.bufferProvider.getBuffer = NULL;
598 pDwmModule->config.outputCfg.bufferProvider.releaseBuffer = NULL;
599 pDwmModule->config.outputCfg.bufferProvider.cookie = NULL;
600 pDwmModule->config.outputCfg.mask = EFFECT_CONFIG_ALL;
601
602 ret = Downmix_Configure(pDwmModule, &pDwmModule->config, true);
603 if (ret != 0) {
604 ALOGV("Downmix_Init error %d on module %p", ret, pDwmModule);
605 } else {
606 pDwmModule->context.state = DOWNMIX_STATE_INITIALIZED;
607 }
608
609 return ret;
610}
611
612
613/*----------------------------------------------------------------------------
614 * Downmix_Configure()
615 *----------------------------------------------------------------------------
616 * Purpose:
617 * Set input and output audio configuration.
618 *
619 * Inputs:
620 * pDwmModule pointer to downmix effect module
621 * pConfig pointer to effect_config_t structure containing input
622 * and output audio parameters configuration
623 * init true if called from init function
624 *
625 * Outputs:
626 *
627 * Returns:
628 * 0 indicates success
629 *
630 * Side Effects:
631 *
632 *----------------------------------------------------------------------------
633 */
634
635int Downmix_Configure(downmix_module_t *pDwmModule, effect_config_t *pConfig, bool init) {
636
637 downmix_object_t *pDownmixer = &pDwmModule->context;
638
639 // Check configuration compatibility with build options, and effect capabilities
640 if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate
641 || pConfig->outputCfg.channels != DOWNMIX_OUTPUT_CHANNELS
642 || pConfig->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT
643 || pConfig->outputCfg.format != AUDIO_FORMAT_PCM_16_BIT) {
644 ALOGE("Downmix_Configure error: invalid config");
645 return -EINVAL;
646 }
647
Chih-Wei Huanga93c8c92013-01-10 20:52:57 +0800648 if (&pDwmModule->config != pConfig) {
649 memcpy(&pDwmModule->config, pConfig, sizeof(effect_config_t));
650 }
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800651
652 if (init) {
653 pDownmixer->type = DOWNMIX_TYPE_FOLD;
654 pDownmixer->apply_volume_correction = false;
655 pDownmixer->input_channel_count = 8; // matches default input of AUDIO_CHANNEL_OUT_7POINT1
656 } else {
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -0700657 // when configuring the effect, do not allow a blank or unsupported channel mask
658 if (!Downmix_validChannelMask(pConfig->inputCfg.channels)) {
659 ALOGE("Downmix_Configure error: input channel mask(0x%x) not supported",
660 pConfig->inputCfg.channels);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800661 return -EINVAL;
662 }
Andy Hunge5412692014-05-16 11:25:07 -0700663 pDownmixer->input_channel_count =
664 audio_channel_count_from_out_mask(pConfig->inputCfg.channels);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800665 }
666
667 Downmix_Reset(pDownmixer, init);
668
669 return 0;
670}
671
672
673/*----------------------------------------------------------------------------
674 * Downmix_Reset()
675 *----------------------------------------------------------------------------
676 * Purpose:
677 * Reset internal states.
678 *
679 * Inputs:
680 * pDownmixer pointer to downmix context
681 * init true if called from init function
682 *
683 * Outputs:
684*
685 * Returns:
686 * 0 indicates success
687 *
688 * Side Effects:
689 *
690 *----------------------------------------------------------------------------
691 */
692
Eric Laurent0f714a42015-06-19 15:33:57 -0700693int Downmix_Reset(downmix_object_t *pDownmixer __unused, bool init __unused) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800694 // nothing to do here
695 return 0;
696}
697
698
699/*----------------------------------------------------------------------------
700 * Downmix_setParameter()
701 *----------------------------------------------------------------------------
702 * Purpose:
703 * Set a Downmix parameter
704 *
705 * Inputs:
706 * pDownmixer handle to instance data
707 * param parameter
708 * pValue pointer to parameter value
709 * size value size
710 *
711 * Outputs:
712 *
713 * Returns:
714 * 0 indicates success
715 *
716 * Side Effects:
717 *
718 *----------------------------------------------------------------------------
719 */
Ashok Bhatb302bd52014-02-18 11:40:00 +0000720int Downmix_setParameter(downmix_object_t *pDownmixer, int32_t param, uint32_t size, void *pValue) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800721
722 int16_t value16;
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700723 ALOGV("Downmix_setParameter, context %p, param %" PRId32 ", value16 %" PRId16 ", value32 %" PRId32,
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800724 pDownmixer, param, *(int16_t *)pValue, *(int32_t *)pValue);
725
726 switch (param) {
727
728 case DOWNMIX_PARAM_TYPE:
729 if (size != sizeof(downmix_type_t)) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700730 ALOGE("Downmix_setParameter(DOWNMIX_PARAM_TYPE) invalid size %" PRIu32 ", should be %zu",
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800731 size, sizeof(downmix_type_t));
732 return -EINVAL;
733 }
734 value16 = *(int16_t *)pValue;
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700735 ALOGV("set DOWNMIX_PARAM_TYPE, type %" PRId16, value16);
Jean-Michel Trivi7d5b2622012-04-04 18:54:36 -0700736 if (!((value16 > DOWNMIX_TYPE_INVALID) && (value16 <= DOWNMIX_TYPE_LAST))) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700737 ALOGE("Downmix_setParameter invalid DOWNMIX_PARAM_TYPE value %" PRId16, value16);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800738 return -EINVAL;
739 } else {
740 pDownmixer->type = (downmix_type_t) value16;
741 break;
742
743 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700744 ALOGE("Downmix_setParameter unknown parameter %" PRId32, param);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800745 return -EINVAL;
746 }
747}
748
749 return 0;
750} /* end Downmix_setParameter */
751
752
753/*----------------------------------------------------------------------------
754 * Downmix_getParameter()
755 *----------------------------------------------------------------------------
756 * Purpose:
757 * Get a Downmix parameter
758 *
759 * Inputs:
760 * pDownmixer handle to instance data
761 * param parameter
762 * pValue pointer to variable to hold retrieved value
763 * pSize pointer to value size: maximum size as input
764 *
765 * Outputs:
766 * *pValue updated with parameter value
767 * *pSize updated with actual value size
768 *
769 * Returns:
770 * 0 indicates success
771 *
772 * Side Effects:
773 *
774 *----------------------------------------------------------------------------
775 */
Ashok Bhatb302bd52014-02-18 11:40:00 +0000776int Downmix_getParameter(downmix_object_t *pDownmixer, int32_t param, uint32_t *pSize, void *pValue) {
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800777 int16_t *pValue16;
778
779 switch (param) {
780
781 case DOWNMIX_PARAM_TYPE:
782 if (*pSize < sizeof(int16_t)) {
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700783 ALOGE("Downmix_getParameter invalid parameter size %" PRIu32 " for DOWNMIX_PARAM_TYPE", *pSize);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800784 return -EINVAL;
785 }
786 pValue16 = (int16_t *)pValue;
787 *pValue16 = (int16_t) pDownmixer->type;
788 *pSize = sizeof(int16_t);
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700789 ALOGV("Downmix_getParameter DOWNMIX_PARAM_TYPE is %" PRId16, *pValue16);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800790 break;
791
792 default:
Mark Salyzyn7cb0e732014-04-18 13:48:25 -0700793 ALOGE("Downmix_getParameter unknown parameter %" PRId16, param);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800794 return -EINVAL;
795 }
796
797 return 0;
798} /* end Downmix_getParameter */
799
800
801/*----------------------------------------------------------------------------
802 * Downmix_foldFromQuad()
803 *----------------------------------------------------------------------------
804 * Purpose:
805 * downmix a quad signal to stereo
806 *
807 * Inputs:
808 * pSrc quad audio samples to downmix
809 * numFrames the number of quad frames to downmix
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700810 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
811 * or overwrite pDst (when false)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800812 *
813 * Outputs:
814 * pDst downmixed stereo audio samples
815 *
816 *----------------------------------------------------------------------------
817 */
818void Downmix_foldFromQuad(int16_t *pSrc, int16_t*pDst, size_t numFrames, bool accumulate) {
819 // sample at index 0 is FL
820 // sample at index 1 is FR
821 // sample at index 2 is RL
822 // sample at index 3 is RR
823 if (accumulate) {
824 while (numFrames) {
825 // FL + RL
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700826 pDst[0] = clamp16(pDst[0] + ((pSrc[0] + pSrc[2]) >> 1));
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800827 // FR + RR
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700828 pDst[1] = clamp16(pDst[1] + ((pSrc[1] + pSrc[3]) >> 1));
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800829 pSrc += 4;
830 pDst += 2;
831 numFrames--;
832 }
833 } else { // same code as above but without adding and clamping pDst[i] to itself
834 while (numFrames) {
835 // FL + RL
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700836 pDst[0] = clamp16((pSrc[0] + pSrc[2]) >> 1);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800837 // FR + RR
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700838 pDst[1] = clamp16((pSrc[1] + pSrc[3]) >> 1);
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800839 pSrc += 4;
840 pDst += 2;
841 numFrames--;
842 }
843 }
844}
845
846
847/*----------------------------------------------------------------------------
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800848 * Downmix_foldFrom5Point1()
849 *----------------------------------------------------------------------------
850 * Purpose:
851 * downmix a 5.1 signal to stereo
852 *
853 * Inputs:
854 * pSrc 5.1 audio samples to downmix
855 * numFrames the number of 5.1 frames to downmix
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700856 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
857 * or overwrite pDst (when false)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800858 *
859 * Outputs:
860 * pDst downmixed stereo audio samples
861 *
862 *----------------------------------------------------------------------------
863 */
864void Downmix_foldFrom5Point1(int16_t *pSrc, int16_t*pDst, size_t numFrames, bool accumulate) {
865 int32_t lt, rt, centerPlusLfeContrib; // samples in Q19.12 format
866 // sample at index 0 is FL
867 // sample at index 1 is FR
868 // sample at index 2 is FC
869 // sample at index 3 is LFE
870 // sample at index 4 is RL
871 // sample at index 5 is RR
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700872 // code is mostly duplicated between the two values of accumulate to avoid repeating the test
873 // for every sample
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800874 if (accumulate) {
875 while (numFrames) {
876 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
877 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_Q19_12)
878 + (pSrc[3] * MINUS_3_DB_IN_Q19_12);
879 // FL + centerPlusLfeContrib + RL
880 lt = (pSrc[0] << 12) + centerPlusLfeContrib + (pSrc[4] << 12);
881 // FR + centerPlusLfeContrib + RR
882 rt = (pSrc[1] << 12) + centerPlusLfeContrib + (pSrc[5] << 12);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700883 // accumulate in destination
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700884 pDst[0] = clamp16(pDst[0] + (lt >> 13));
885 pDst[1] = clamp16(pDst[1] + (rt >> 13));
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800886 pSrc += 6;
887 pDst += 2;
888 numFrames--;
889 }
890 } else { // same code as above but without adding and clamping pDst[i] to itself
891 while (numFrames) {
892 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
893 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_Q19_12)
894 + (pSrc[3] * MINUS_3_DB_IN_Q19_12);
895 // FL + centerPlusLfeContrib + RL
896 lt = (pSrc[0] << 12) + centerPlusLfeContrib + (pSrc[4] << 12);
897 // FR + centerPlusLfeContrib + RR
898 rt = (pSrc[1] << 12) + centerPlusLfeContrib + (pSrc[5] << 12);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700899 // store in destination
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700900 pDst[0] = clamp16(lt >> 13); // differs from when accumulate is true above
901 pDst[1] = clamp16(rt >> 13); // differs from when accumulate is true above
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800902 pSrc += 6;
903 pDst += 2;
904 numFrames--;
905 }
906 }
907}
908
909
910/*----------------------------------------------------------------------------
911 * Downmix_foldFrom7Point1()
912 *----------------------------------------------------------------------------
913 * Purpose:
914 * downmix a 7.1 signal to stereo
915 *
916 * Inputs:
917 * pSrc 7.1 audio samples to downmix
918 * numFrames the number of 7.1 frames to downmix
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700919 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
920 * or overwrite pDst (when false)
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800921 *
922 * Outputs:
923 * pDst downmixed stereo audio samples
924 *
925 *----------------------------------------------------------------------------
926 */
927void Downmix_foldFrom7Point1(int16_t *pSrc, int16_t*pDst, size_t numFrames, bool accumulate) {
928 int32_t lt, rt, centerPlusLfeContrib; // samples in Q19.12 format
929 // sample at index 0 is FL
930 // sample at index 1 is FR
931 // sample at index 2 is FC
932 // sample at index 3 is LFE
933 // sample at index 4 is RL
934 // sample at index 5 is RR
935 // sample at index 6 is SL
936 // sample at index 7 is SR
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700937 // code is mostly duplicated between the two values of accumulate to avoid repeating the test
938 // for every sample
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800939 if (accumulate) {
940 while (numFrames) {
941 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
942 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_Q19_12)
943 + (pSrc[3] * MINUS_3_DB_IN_Q19_12);
944 // FL + centerPlusLfeContrib + SL + RL
945 lt = (pSrc[0] << 12) + centerPlusLfeContrib + (pSrc[6] << 12) + (pSrc[4] << 12);
946 // FR + centerPlusLfeContrib + SR + RR
947 rt = (pSrc[1] << 12) + centerPlusLfeContrib + (pSrc[7] << 12) + (pSrc[5] << 12);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700948 //accumulate in destination
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700949 pDst[0] = clamp16(pDst[0] + (lt >> 13));
950 pDst[1] = clamp16(pDst[1] + (rt >> 13));
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800951 pSrc += 8;
952 pDst += 2;
953 numFrames--;
954 }
955 } else { // same code as above but without adding and clamping pDst[i] to itself
956 while (numFrames) {
957 // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
958 centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_Q19_12)
959 + (pSrc[3] * MINUS_3_DB_IN_Q19_12);
960 // FL + centerPlusLfeContrib + SL + RL
961 lt = (pSrc[0] << 12) + centerPlusLfeContrib + (pSrc[6] << 12) + (pSrc[4] << 12);
962 // FR + centerPlusLfeContrib + SR + RR
963 rt = (pSrc[1] << 12) + centerPlusLfeContrib + (pSrc[7] << 12) + (pSrc[5] << 12);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700964 // store in destination
Jean-Michel Triviaea27152012-05-24 10:11:07 -0700965 pDst[0] = clamp16(lt >> 13); // differs from when accumulate is true above
966 pDst[1] = clamp16(rt >> 13); // differs from when accumulate is true above
Jean-Michel Trivi04c1e532012-03-02 10:59:56 -0800967 pSrc += 8;
968 pDst += 2;
969 numFrames--;
970 }
971 }
972}
973
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -0700974
975/*----------------------------------------------------------------------------
976 * Downmix_foldGeneric()
977 *----------------------------------------------------------------------------
978 * Purpose:
979 * downmix to stereo a multichannel signal whose format is:
980 * - has FL/FR
981 * - if using AUDIO_CHANNEL_OUT_SIDE*, it contains both left and right
982 * - if using AUDIO_CHANNEL_OUT_BACK*, it contains both left and right
983 * - doesn't use any of the AUDIO_CHANNEL_OUT_TOP* channels
984 * - doesn't use any of the AUDIO_CHANNEL_OUT_FRONT_*_OF_CENTER channels
985 * Only handles channel masks not enumerated in downmix_input_channel_mask_t
986 *
987 * Inputs:
988 * mask the channel mask of pSrc
989 * pSrc multichannel audio buffer to downmix
990 * numFrames the number of multichannel frames to downmix
991 * accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
992 * or overwrite pDst (when false)
993 *
994 * Outputs:
995 * pDst downmixed stereo audio samples
996 *
997 * Returns: false if multichannel format is not supported
998 *
999 *----------------------------------------------------------------------------
1000 */
1001bool Downmix_foldGeneric(
1002 uint32_t mask, int16_t *pSrc, int16_t*pDst, size_t numFrames, bool accumulate) {
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -07001003
1004 if (!Downmix_validChannelMask(mask)) {
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -07001005 return false;
1006 }
Haynes Mathew Georgeee507ef2015-07-15 11:06:05 -07001007
1008 const bool hasSides = (mask & kSides) != 0;
1009 const bool hasBacks = (mask & kBacks) != 0;
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -07001010
Andy Hunge5412692014-05-16 11:25:07 -07001011 const int numChan = audio_channel_count_from_out_mask(mask);
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -07001012 const bool hasFC = ((mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) == AUDIO_CHANNEL_OUT_FRONT_CENTER);
1013 const bool hasLFE =
1014 ((mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) == AUDIO_CHANNEL_OUT_LOW_FREQUENCY);
1015 const bool hasBC = ((mask & AUDIO_CHANNEL_OUT_BACK_CENTER) == AUDIO_CHANNEL_OUT_BACK_CENTER);
1016 // compute at what index each channel is: samples will be in the following order:
1017 // FL FR FC LFE BL BR BC SL SR
1018 // when a channel is not present, its index is set to the same as the index of the preceding
1019 // channel
1020 const int indexFC = hasFC ? 2 : 1; // front center
1021 const int indexLFE = hasLFE ? indexFC + 1 : indexFC; // low frequency
1022 const int indexBL = hasBacks ? indexLFE + 1 : indexLFE; // back left
1023 const int indexBR = hasBacks ? indexBL + 1 : indexBL; // back right
1024 const int indexBC = hasBC ? indexBR + 1 : indexBR; // back center
1025 const int indexSL = hasSides ? indexBC + 1 : indexBC; // side left
1026 const int indexSR = hasSides ? indexSL + 1 : indexSL; // side right
1027
1028 int32_t lt, rt, centersLfeContrib; // samples in Q19.12 format
1029 // code is mostly duplicated between the two values of accumulate to avoid repeating the test
1030 // for every sample
1031 if (accumulate) {
1032 while (numFrames) {
1033 // compute contribution of FC, BC and LFE
1034 centersLfeContrib = 0;
1035 if (hasFC) { centersLfeContrib += pSrc[indexFC]; }
1036 if (hasLFE) { centersLfeContrib += pSrc[indexLFE]; }
1037 if (hasBC) { centersLfeContrib += pSrc[indexBC]; }
1038 centersLfeContrib *= MINUS_3_DB_IN_Q19_12;
1039 // always has FL/FR
1040 lt = (pSrc[0] << 12);
1041 rt = (pSrc[1] << 12);
1042 // mix in sides and backs
1043 if (hasSides) {
1044 lt += pSrc[indexSL] << 12;
1045 rt += pSrc[indexSR] << 12;
1046 }
1047 if (hasBacks) {
1048 lt += pSrc[indexBL] << 12;
1049 rt += pSrc[indexBR] << 12;
1050 }
1051 lt += centersLfeContrib;
1052 rt += centersLfeContrib;
1053 // accumulate in destination
Jean-Michel Triviaea27152012-05-24 10:11:07 -07001054 pDst[0] = clamp16(pDst[0] + (lt >> 13));
1055 pDst[1] = clamp16(pDst[1] + (rt >> 13));
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -07001056 pSrc += numChan;
1057 pDst += 2;
1058 numFrames--;
1059 }
1060 } else {
1061 while (numFrames) {
1062 // compute contribution of FC, BC and LFE
1063 centersLfeContrib = 0;
1064 if (hasFC) { centersLfeContrib += pSrc[indexFC]; }
1065 if (hasLFE) { centersLfeContrib += pSrc[indexLFE]; }
1066 if (hasBC) { centersLfeContrib += pSrc[indexBC]; }
1067 centersLfeContrib *= MINUS_3_DB_IN_Q19_12;
1068 // always has FL/FR
1069 lt = (pSrc[0] << 12);
1070 rt = (pSrc[1] << 12);
1071 // mix in sides and backs
1072 if (hasSides) {
1073 lt += pSrc[indexSL] << 12;
1074 rt += pSrc[indexSR] << 12;
1075 }
1076 if (hasBacks) {
1077 lt += pSrc[indexBL] << 12;
1078 rt += pSrc[indexBR] << 12;
1079 }
1080 lt += centersLfeContrib;
1081 rt += centersLfeContrib;
1082 // store in destination
Jean-Michel Triviaea27152012-05-24 10:11:07 -07001083 pDst[0] = clamp16(lt >> 13); // differs from when accumulate is true above
1084 pDst[1] = clamp16(rt >> 13); // differs from when accumulate is true above
Jean-Michel Trivi6895dee2012-05-15 15:51:16 -07001085 pSrc += numChan;
1086 pDst += 2;
1087 numFrames--;
1088 }
1089 }
1090 return true;
1091}