• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
18 //#define LOG_NDEBUG 0
19 #include <log/log.h>
20 
21 #include "EffectDownmix.h"
22 #include <math.h>
23 
24 // Do not submit with DOWNMIX_TEST_CHANNEL_INDEX defined, strictly for testing
25 //#define DOWNMIX_TEST_CHANNEL_INDEX 0
26 // Do not submit with DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER defined, strictly for testing
27 //#define DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER 0
28 
29 #define MINUS_3_DB_IN_FLOAT M_SQRT1_2 // -3dB = 0.70710678
30 
31 typedef enum {
32     DOWNMIX_STATE_UNINITIALIZED,
33     DOWNMIX_STATE_INITIALIZED,
34     DOWNMIX_STATE_ACTIVE,
35 } downmix_state_t;
36 
37 /* parameters for each downmixer */
38 typedef struct {
39     downmix_state_t state;
40     downmix_type_t type;
41     bool apply_volume_correction;
42     uint8_t input_channel_count;
43 } downmix_object_t;
44 
45 typedef struct downmix_module_s {
46     const struct effect_interface_s *itfe;
47     effect_config_t config;
48     downmix_object_t context;
49 } downmix_module_t;
50 
51 
52 // Audio Effect API
53 static int32_t DownmixLib_Create(const effect_uuid_t *uuid,
54         int32_t sessionId,
55         int32_t ioId,
56         effect_handle_t *pHandle);
57 static int32_t DownmixLib_Release(effect_handle_t handle);
58 static int32_t DownmixLib_GetDescriptor(const effect_uuid_t *uuid,
59         effect_descriptor_t *pDescriptor);
60 static int32_t Downmix_Process(effect_handle_t self,
61         audio_buffer_t *inBuffer,
62         audio_buffer_t *outBuffer);
63 static int32_t Downmix_Command(effect_handle_t self,
64         uint32_t cmdCode,
65         uint32_t cmdSize,
66         void *pCmdData,
67         uint32_t *replySize,
68         void *pReplyData);
69 static int32_t Downmix_GetDescriptor(effect_handle_t self,
70         effect_descriptor_t *pDescriptor);
71 
72 // Internal methods
73 static int Downmix_Init(downmix_module_t *pDwmModule);
74 static int Downmix_Configure(downmix_module_t *pDwmModule, effect_config_t *pConfig, bool init);
75 static int Downmix_Reset(downmix_object_t *pDownmixer, bool init);
76 static int Downmix_setParameter(
77         downmix_object_t *pDownmixer, int32_t param, uint32_t size, void *pValue);
78 static int Downmix_getParameter(
79         downmix_object_t *pDownmixer, int32_t param, uint32_t *pSize, void *pValue);
80 static void Downmix_foldFromQuad(float *pSrc, float *pDst, size_t numFrames, bool accumulate);
81 static void Downmix_foldFrom5Point1(float *pSrc, float *pDst, size_t numFrames, bool accumulate);
82 static void Downmix_foldFrom7Point1(float *pSrc, float *pDst, size_t numFrames, bool accumulate);
83 static bool Downmix_foldGeneric(
84         uint32_t mask, float *pSrc, float *pDst, size_t numFrames, bool accumulate);
85 
86 // effect_handle_t interface implementation for downmix effect
87 const struct effect_interface_s gDownmixInterface = {
88         Downmix_Process,
89         Downmix_Command,
90         Downmix_GetDescriptor,
91         NULL /* no process_reverse function, no reference stream needed */
92 };
93 
94 // This is the only symbol that needs to be exported
95 __attribute__ ((visibility ("default")))
96 audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
97     .tag = AUDIO_EFFECT_LIBRARY_TAG,
98     .version = EFFECT_LIBRARY_API_VERSION,
99     .name = "Downmix Library",
100     .implementor = "The Android Open Source Project",
101     .create_effect = DownmixLib_Create,
102     .release_effect = DownmixLib_Release,
103     .get_descriptor = DownmixLib_GetDescriptor,
104 };
105 
106 // AOSP insert downmix UUID: 93f04452-e4fe-41cc-91f9-e475b6d1d69f
107 static const effect_descriptor_t gDownmixDescriptor = {
108         EFFECT_UIID_DOWNMIX__, //type
109         {0x93f04452, 0xe4fe, 0x41cc, 0x91f9, {0xe4, 0x75, 0xb6, 0xd1, 0xd6, 0x9f}}, // uuid
110         EFFECT_CONTROL_API_VERSION,
111         EFFECT_FLAG_TYPE_INSERT | EFFECT_FLAG_INSERT_FIRST,
112         0, //FIXME what value should be reported? // cpu load
113         0, //FIXME what value should be reported? // memory usage
114         "Multichannel Downmix To Stereo", // human readable effect name
115         "The Android Open Source Project" // human readable effect implementor name
116 };
117 
118 // gDescriptors contains pointers to all defined effect descriptor in this library
119 static const effect_descriptor_t * const gDescriptors[] = {
120         &gDownmixDescriptor
121 };
122 
123 // number of effects in this library
124 const int kNbEffects = sizeof(gDescriptors) / sizeof(const effect_descriptor_t *);
125 
clamp_float(float value)126 static inline float clamp_float(float value) {
127     return fmin(fmax(value, -1.f), 1.f);
128 }
129 
130 /*----------------------------------------------------------------------------
131  * Test code
132  *--------------------------------------------------------------------------*/
133 #ifdef DOWNMIX_TEST_CHANNEL_INDEX
134 // strictly for testing, logs the indices of the channels for a given mask,
135 // uses the same code as Downmix_foldGeneric()
Downmix_testIndexComputation(uint32_t mask)136 void Downmix_testIndexComputation(uint32_t mask) {
137     ALOGI("Testing index computation for %#x:", mask);
138     // check against unsupported channels
139     if (mask & kUnsupported) {
140         ALOGE("Unsupported channels (top or front left/right of center)");
141         return;
142     }
143     // verify has FL/FR
144     if ((mask & AUDIO_CHANNEL_OUT_STEREO) != AUDIO_CHANNEL_OUT_STEREO) {
145         ALOGE("Front channels must be present");
146         return;
147     }
148     // verify uses SIDE as a pair (ok if not using SIDE at all)
149     bool hasSides = false;
150     if ((mask & kSides) != 0) {
151         if ((mask & kSides) != kSides) {
152             ALOGE("Side channels must be used as a pair");
153             return;
154         }
155         hasSides = true;
156     }
157     // verify uses BACK as a pair (ok if not using BACK at all)
158     bool hasBacks = false;
159     if ((mask & kBacks) != 0) {
160         if ((mask & kBacks) != kBacks) {
161             ALOGE("Back channels must be used as a pair");
162             return;
163         }
164         hasBacks = true;
165     }
166 
167     const int numChan = audio_channel_count_from_out_mask(mask);
168     const bool hasFC = ((mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) == AUDIO_CHANNEL_OUT_FRONT_CENTER);
169     const bool hasLFE =
170             ((mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) == AUDIO_CHANNEL_OUT_LOW_FREQUENCY);
171     const bool hasBC = ((mask & AUDIO_CHANNEL_OUT_BACK_CENTER) == AUDIO_CHANNEL_OUT_BACK_CENTER);
172     // compute at what index each channel is: samples will be in the following order:
173     //   FL FR FC LFE BL BR BC SL SR
174     // when a channel is not present, its index is set to the same as the index of the preceding
175     // channel
176     const int indexFC  = hasFC    ? 2            : 1;        // front center
177     const int indexLFE = hasLFE   ? indexFC + 1  : indexFC;  // low frequency
178     const int indexBL  = hasBacks ? indexLFE + 1 : indexLFE; // back left
179     const int indexBR  = hasBacks ? indexBL + 1  : indexBL;  // back right
180     const int indexBC  = hasBC    ? indexBR + 1  : indexBR;  // back center
181     const int indexSL  = hasSides ? indexBC + 1  : indexBC;  // side left
182     const int indexSR  = hasSides ? indexSL + 1  : indexSL;  // side right
183 
184     ALOGI("  FL FR FC LFE BL BR BC SL SR");
185     ALOGI("   %d  %d  %d   %d  %d  %d  %d  %d  %d",
186             0, 1, indexFC, indexLFE, indexBL, indexBR, indexBC, indexSL, indexSR);
187 }
188 #endif
189 
Downmix_validChannelMask(uint32_t mask)190 static bool Downmix_validChannelMask(uint32_t mask)
191 {
192     if (!mask) {
193         return false;
194     }
195     // check against unsupported channels
196     if (mask & ~AUDIO_CHANNEL_OUT_22POINT2) {
197         ALOGE("Unsupported channels in %u", mask & ~AUDIO_CHANNEL_OUT_22POINT2);
198         return false;
199     }
200     return true;
201 }
202 
203 /*----------------------------------------------------------------------------
204  * Effect API implementation
205  *--------------------------------------------------------------------------*/
206 
207 /*--- Effect Library Interface Implementation ---*/
208 
DownmixLib_Create(const effect_uuid_t * uuid,int32_t,int32_t,effect_handle_t * pHandle)209 static int32_t DownmixLib_Create(const effect_uuid_t *uuid,
210         int32_t /* sessionId */,
211         int32_t /* ioId */,
212         effect_handle_t *pHandle) {
213     int ret;
214     int i;
215     downmix_module_t *module;
216     const effect_descriptor_t *desc;
217 
218     ALOGV("DownmixLib_Create()");
219 
220 #ifdef DOWNMIX_TEST_CHANNEL_INDEX
221     // should work (won't log an error)
222     ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should work:");
223     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
224                     AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_CENTER);
225     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_QUAD_SIDE | AUDIO_CHANNEL_OUT_QUAD_BACK);
226     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_5POINT1_SIDE | AUDIO_CHANNEL_OUT_BACK_CENTER);
227     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_5POINT1_BACK | AUDIO_CHANNEL_OUT_BACK_CENTER);
228     // shouldn't work (will log an error, won't display channel indices)
229     ALOGI("DOWNMIX_TEST_CHANNEL_INDEX: should NOT work:");
230     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
231                         AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_BACK_LEFT);
232     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT | AUDIO_CHANNEL_OUT_FRONT_RIGHT |
233                             AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_SIDE_LEFT);
234     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
235                         AUDIO_CHANNEL_OUT_BACK_LEFT | AUDIO_CHANNEL_OUT_BACK_RIGHT);
236     Downmix_testIndexComputation(AUDIO_CHANNEL_OUT_FRONT_LEFT |
237                             AUDIO_CHANNEL_OUT_SIDE_LEFT | AUDIO_CHANNEL_OUT_SIDE_RIGHT);
238 #endif
239 
240     if (pHandle == NULL || uuid == NULL) {
241         return -EINVAL;
242     }
243 
244     for (i = 0 ; i < kNbEffects ; i++) {
245         desc = gDescriptors[i];
246         if (memcmp(uuid, &desc->uuid, sizeof(effect_uuid_t)) == 0) {
247             break;
248         }
249     }
250 
251     if (i == kNbEffects) {
252         return -ENOENT;
253     }
254 
255     module = new downmix_module_t{};
256 
257     module->itfe = &gDownmixInterface;
258 
259     module->context.state = DOWNMIX_STATE_UNINITIALIZED;
260 
261     ret = Downmix_Init(module);
262     if (ret < 0) {
263         ALOGW("DownmixLib_Create() init failed");
264         free(module);
265         return ret;
266     }
267 
268     *pHandle = (effect_handle_t) module;
269 
270     ALOGV("DownmixLib_Create() %p , size %zu", module, sizeof(downmix_module_t));
271 
272     return 0;
273 }
274 
DownmixLib_Release(effect_handle_t handle)275 static int32_t DownmixLib_Release(effect_handle_t handle) {
276     downmix_module_t *pDwmModule = (downmix_module_t *)handle;
277 
278     ALOGV("DownmixLib_Release() %p", handle);
279     if (handle == NULL) {
280         return -EINVAL;
281     }
282 
283     pDwmModule->context.state = DOWNMIX_STATE_UNINITIALIZED;
284 
285     delete pDwmModule;
286     return 0;
287 }
288 
DownmixLib_GetDescriptor(const effect_uuid_t * uuid,effect_descriptor_t * pDescriptor)289 static int32_t DownmixLib_GetDescriptor(
290         const effect_uuid_t *uuid, effect_descriptor_t *pDescriptor) {
291     ALOGV("DownmixLib_GetDescriptor()");
292     int i;
293 
294     if (pDescriptor == NULL || uuid == NULL){
295         ALOGE("DownmixLib_Create() called with NULL pointer");
296         return -EINVAL;
297     }
298     ALOGV("DownmixLib_GetDescriptor() nb effects=%d", kNbEffects);
299     for (i = 0; i < kNbEffects; i++) {
300         ALOGV("DownmixLib_GetDescriptor() i=%d", i);
301         if (memcmp(uuid, &gDescriptors[i]->uuid, sizeof(effect_uuid_t)) == 0) {
302             memcpy(pDescriptor, gDescriptors[i], sizeof(effect_descriptor_t));
303             ALOGV("EffectGetDescriptor - UUID matched downmix type %d, UUID = %#x",
304                  i, gDescriptors[i]->uuid.timeLow);
305             return 0;
306         }
307     }
308 
309     return -EINVAL;
310 }
311 
312 /*--- Effect Control Interface Implementation ---*/
313 
Downmix_Process(effect_handle_t self,audio_buffer_t * inBuffer,audio_buffer_t * outBuffer)314 static int32_t Downmix_Process(effect_handle_t self,
315         audio_buffer_t *inBuffer, audio_buffer_t *outBuffer) {
316 
317     downmix_object_t *pDownmixer;
318     float *pSrc, *pDst;
319     downmix_module_t *pDwmModule = (downmix_module_t *)self;
320 
321     if (pDwmModule == NULL) {
322         return -EINVAL;
323     }
324 
325     if (inBuffer == NULL || inBuffer->raw == NULL ||
326         outBuffer == NULL || outBuffer->raw == NULL ||
327         inBuffer->frameCount != outBuffer->frameCount) {
328         return -EINVAL;
329     }
330 
331     pDownmixer = (downmix_object_t*) &pDwmModule->context;
332 
333     if (pDownmixer->state == DOWNMIX_STATE_UNINITIALIZED) {
334         ALOGE("Downmix_Process error: trying to use an uninitialized downmixer");
335         return -EINVAL;
336     } else if (pDownmixer->state == DOWNMIX_STATE_INITIALIZED) {
337         ALOGE("Downmix_Process error: trying to use a non-configured downmixer");
338         return -ENODATA;
339     }
340 
341     pSrc = inBuffer->f32;
342     pDst = outBuffer->f32;
343     size_t numFrames = outBuffer->frameCount;
344 
345     const bool accumulate =
346             (pDwmModule->config.outputCfg.accessMode == EFFECT_BUFFER_ACCESS_ACCUMULATE);
347     const uint32_t downmixInputChannelMask = pDwmModule->config.inputCfg.channels;
348 
349     switch(pDownmixer->type) {
350 
351       case DOWNMIX_TYPE_STRIP:
352           if (accumulate) {
353               while (numFrames) {
354                   pDst[0] = clamp_float(pDst[0] + pSrc[0]);
355                   pDst[1] = clamp_float(pDst[1] + pSrc[1]);
356                   pSrc += pDownmixer->input_channel_count;
357                   pDst += 2;
358                   numFrames--;
359               }
360           } else {
361               while (numFrames) {
362                   pDst[0] = pSrc[0];
363                   pDst[1] = pSrc[1];
364                   pSrc += pDownmixer->input_channel_count;
365                   pDst += 2;
366                   numFrames--;
367               }
368           }
369           break;
370 
371       case DOWNMIX_TYPE_FOLD:
372 #ifdef DOWNMIX_ALWAYS_USE_GENERIC_DOWNMIXER
373           // bypass the optimized downmix routines for the common formats
374           if (!Downmix_foldGeneric(
375                   downmixInputChannelMask, pSrc, pDst, numFrames, accumulate)) {
376               ALOGE("Multichannel configuration %#x is not supported",
377                     downmixInputChannelMask);
378               return -EINVAL;
379           }
380           break;
381 #endif
382         // optimize for the common formats
383         switch (downmixInputChannelMask) {
384         case AUDIO_CHANNEL_OUT_QUAD_BACK:
385         case AUDIO_CHANNEL_OUT_QUAD_SIDE:
386             Downmix_foldFromQuad(pSrc, pDst, numFrames, accumulate);
387             break;
388         case AUDIO_CHANNEL_OUT_5POINT1_BACK:
389         case AUDIO_CHANNEL_OUT_5POINT1_SIDE:
390             Downmix_foldFrom5Point1(pSrc, pDst, numFrames, accumulate);
391             break;
392         case AUDIO_CHANNEL_OUT_7POINT1:
393             Downmix_foldFrom7Point1(pSrc, pDst, numFrames, accumulate);
394             break;
395         default:
396             if (!Downmix_foldGeneric(
397                     downmixInputChannelMask, pSrc, pDst, numFrames, accumulate)) {
398                 ALOGE("Multichannel configuration %#x is not supported",
399                       downmixInputChannelMask);
400                 return -EINVAL;
401             }
402             break;
403         }
404         break;
405 
406       default:
407         return -EINVAL;
408     }
409 
410     return 0;
411 }
412 
Downmix_Command(effect_handle_t self,uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)413 static int32_t Downmix_Command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
414         void *pCmdData, uint32_t *replySize, void *pReplyData) {
415 
416     downmix_module_t *pDwmModule = (downmix_module_t *) self;
417     downmix_object_t *pDownmixer;
418 
419     if (pDwmModule == NULL || pDwmModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
420         return -EINVAL;
421     }
422 
423     pDownmixer = (downmix_object_t*) &pDwmModule->context;
424 
425     ALOGV("Downmix_Command command %u cmdSize %u", cmdCode, cmdSize);
426 
427     switch (cmdCode) {
428     case EFFECT_CMD_INIT:
429         if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
430             return -EINVAL;
431         }
432         *(int *) pReplyData = Downmix_Init(pDwmModule);
433         break;
434 
435     case EFFECT_CMD_SET_CONFIG:
436         if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
437                 || pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
438             return -EINVAL;
439         }
440         *(int *) pReplyData = Downmix_Configure(pDwmModule,
441                 (effect_config_t *)pCmdData, false);
442         break;
443 
444     case EFFECT_CMD_RESET:
445         Downmix_Reset(pDownmixer, false);
446         break;
447 
448     case EFFECT_CMD_GET_PARAM: {
449         ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM pCmdData %p, *replySize %u, pReplyData: %p",
450                 pCmdData, *replySize, pReplyData);
451         if (pCmdData == NULL || cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)) ||
452                 pReplyData == NULL || replySize == NULL ||
453                 *replySize < (int) sizeof(effect_param_t) + 2 * sizeof(int32_t)) {
454             return -EINVAL;
455         }
456         effect_param_t *rep = (effect_param_t *) pReplyData;
457         memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + sizeof(int32_t));
458         ALOGV("Downmix_Command EFFECT_CMD_GET_PARAM param %d, replySize %u",
459                 *(int32_t *)rep->data, rep->vsize);
460         rep->status = Downmix_getParameter(pDownmixer, *(int32_t *)rep->data, &rep->vsize,
461                 rep->data + sizeof(int32_t));
462         *replySize = sizeof(effect_param_t) + sizeof(int32_t) + rep->vsize;
463         break;
464     }
465     case EFFECT_CMD_SET_PARAM: {
466         ALOGV("Downmix_Command EFFECT_CMD_SET_PARAM cmdSize %d pCmdData %p, *replySize %u"
467                 ", pReplyData %p", cmdSize, pCmdData, *replySize, pReplyData);
468         if (pCmdData == NULL || (cmdSize < (int)(sizeof(effect_param_t) + sizeof(int32_t)))
469                 || pReplyData == NULL || replySize == NULL || *replySize != (int)sizeof(int32_t)) {
470             return -EINVAL;
471         }
472         effect_param_t *cmd = (effect_param_t *) pCmdData;
473         if (cmd->psize != sizeof(int32_t)) {
474             android_errorWriteLog(0x534e4554, "63662938");
475             return -EINVAL;
476         }
477         *(int *)pReplyData = Downmix_setParameter(pDownmixer, *(int32_t *)cmd->data,
478                 cmd->vsize, cmd->data + sizeof(int32_t));
479         break;
480     }
481 
482     case EFFECT_CMD_SET_PARAM_DEFERRED:
483         //FIXME implement
484         ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_DEFERRED not supported, FIXME");
485         break;
486 
487     case EFFECT_CMD_SET_PARAM_COMMIT:
488         //FIXME implement
489         ALOGW("Downmix_Command command EFFECT_CMD_SET_PARAM_COMMIT not supported, FIXME");
490         break;
491 
492     case EFFECT_CMD_ENABLE:
493         if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
494             return -EINVAL;
495         }
496         if (pDownmixer->state != DOWNMIX_STATE_INITIALIZED) {
497             return -ENOSYS;
498         }
499         pDownmixer->state = DOWNMIX_STATE_ACTIVE;
500         ALOGV("EFFECT_CMD_ENABLE() OK");
501         *(int *)pReplyData = 0;
502         break;
503 
504     case EFFECT_CMD_DISABLE:
505         if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)) {
506             return -EINVAL;
507         }
508         if (pDownmixer->state != DOWNMIX_STATE_ACTIVE) {
509             return -ENOSYS;
510         }
511         pDownmixer->state = DOWNMIX_STATE_INITIALIZED;
512         ALOGV("EFFECT_CMD_DISABLE() OK");
513         *(int *)pReplyData = 0;
514         break;
515 
516     case EFFECT_CMD_SET_DEVICE:
517         if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
518             return -EINVAL;
519         }
520         // FIXME change type if playing on headset vs speaker
521         ALOGV("Downmix_Command EFFECT_CMD_SET_DEVICE: %#x", *(uint32_t *)pCmdData);
522         break;
523 
524     case EFFECT_CMD_SET_VOLUME: {
525         // audio output is always stereo => 2 channel volumes
526         if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t) * 2) {
527             return -EINVAL;
528         }
529         // FIXME change volume
530         ALOGW("Downmix_Command command EFFECT_CMD_SET_VOLUME not supported, FIXME");
531         float left = (float)(*(uint32_t *)pCmdData) / (1 << 24);
532         float right = (float)(*((uint32_t *)pCmdData + 1)) / (1 << 24);
533         ALOGV("Downmix_Command EFFECT_CMD_SET_VOLUME: left %f, right %f ", left, right);
534         break;
535     }
536 
537     case EFFECT_CMD_SET_AUDIO_MODE:
538         if (pCmdData == NULL || cmdSize != (int)sizeof(uint32_t)) {
539             return -EINVAL;
540         }
541         ALOGV("Downmix_Command EFFECT_CMD_SET_AUDIO_MODE: %u", *(uint32_t *)pCmdData);
542         break;
543 
544     case EFFECT_CMD_SET_CONFIG_REVERSE:
545     case EFFECT_CMD_SET_INPUT_DEVICE:
546         // these commands are ignored by a downmix effect
547         break;
548 
549     default:
550         ALOGW("Downmix_Command invalid command %u", cmdCode);
551         return -EINVAL;
552     }
553 
554     return 0;
555 }
556 
Downmix_GetDescriptor(effect_handle_t self,effect_descriptor_t * pDescriptor)557 static int32_t Downmix_GetDescriptor(effect_handle_t self, effect_descriptor_t *pDescriptor)
558 {
559     downmix_module_t *pDwnmxModule = (downmix_module_t *) self;
560 
561     if (pDwnmxModule == NULL ||
562             pDwnmxModule->context.state == DOWNMIX_STATE_UNINITIALIZED) {
563         return -EINVAL;
564     }
565 
566     memcpy(pDescriptor, &gDownmixDescriptor, sizeof(effect_descriptor_t));
567 
568     return 0;
569 }
570 
571 
572 /*----------------------------------------------------------------------------
573  * Downmix internal functions
574  *--------------------------------------------------------------------------*/
575 
576 /*----------------------------------------------------------------------------
577  * Downmix_Init()
578  *----------------------------------------------------------------------------
579  * Purpose:
580  * Initialize downmix context and apply default parameters
581  *
582  * Inputs:
583  *  pDwmModule    pointer to downmix effect module
584  *
585  * Outputs:
586  *
587  * Returns:
588  *  0             indicates success
589  *
590  * Side Effects:
591  *  updates:
592  *           pDwmModule->context.type
593  *           pDwmModule->context.apply_volume_correction
594  *           pDwmModule->config.inputCfg
595  *           pDwmModule->config.outputCfg
596  *           pDwmModule->config.inputCfg.samplingRate
597  *           pDwmModule->config.outputCfg.samplingRate
598  *           pDwmModule->context.state
599  *  doesn't set:
600  *           pDwmModule->itfe
601  *
602  *----------------------------------------------------------------------------
603  */
604 
Downmix_Init(downmix_module_t * pDwmModule)605 static int Downmix_Init(downmix_module_t *pDwmModule) {
606 
607     ALOGV("Downmix_Init module %p", pDwmModule);
608     int ret = 0;
609 
610     memset(&pDwmModule->context, 0, sizeof(downmix_object_t));
611 
612     pDwmModule->config.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
613     pDwmModule->config.inputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
614     pDwmModule->config.inputCfg.channels = AUDIO_CHANNEL_OUT_7POINT1;
615     pDwmModule->config.inputCfg.bufferProvider.getBuffer = NULL;
616     pDwmModule->config.inputCfg.bufferProvider.releaseBuffer = NULL;
617     pDwmModule->config.inputCfg.bufferProvider.cookie = NULL;
618     pDwmModule->config.inputCfg.mask = EFFECT_CONFIG_ALL;
619 
620     pDwmModule->config.inputCfg.samplingRate = 44100;
621     pDwmModule->config.outputCfg.samplingRate = pDwmModule->config.inputCfg.samplingRate;
622 
623     // set a default value for the access mode, but should be overwritten by caller
624     pDwmModule->config.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
625     pDwmModule->config.outputCfg.format = AUDIO_FORMAT_PCM_FLOAT;
626     pDwmModule->config.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
627     pDwmModule->config.outputCfg.bufferProvider.getBuffer = NULL;
628     pDwmModule->config.outputCfg.bufferProvider.releaseBuffer = NULL;
629     pDwmModule->config.outputCfg.bufferProvider.cookie = NULL;
630     pDwmModule->config.outputCfg.mask = EFFECT_CONFIG_ALL;
631 
632     ret = Downmix_Configure(pDwmModule, &pDwmModule->config, true);
633     if (ret != 0) {
634         ALOGV("Downmix_Init error %d on module %p", ret, pDwmModule);
635     } else {
636         pDwmModule->context.state = DOWNMIX_STATE_INITIALIZED;
637     }
638 
639     return ret;
640 }
641 
642 
643 /*----------------------------------------------------------------------------
644  * Downmix_Configure()
645  *----------------------------------------------------------------------------
646  * Purpose:
647  *  Set input and output audio configuration.
648  *
649  * Inputs:
650  *  pDwmModule  pointer to downmix effect module
651  *  pConfig     pointer to effect_config_t structure containing input
652  *                  and output audio parameters configuration
653  *  init        true if called from init function
654  *
655  * Outputs:
656  *
657  * Returns:
658  *  0           indicates success
659  *
660  * Side Effects:
661  *
662  *----------------------------------------------------------------------------
663  */
664 
Downmix_Configure(downmix_module_t * pDwmModule,effect_config_t * pConfig,bool init)665 static int Downmix_Configure(downmix_module_t *pDwmModule, effect_config_t *pConfig, bool init) {
666 
667     downmix_object_t *pDownmixer = &pDwmModule->context;
668 
669     // Check configuration compatibility with build options, and effect capabilities
670     if (pConfig->inputCfg.samplingRate != pConfig->outputCfg.samplingRate
671         || pConfig->outputCfg.channels != AUDIO_CHANNEL_OUT_STEREO
672         || pConfig->inputCfg.format != AUDIO_FORMAT_PCM_FLOAT
673         || pConfig->outputCfg.format != AUDIO_FORMAT_PCM_FLOAT) {
674         ALOGE("Downmix_Configure error: invalid config");
675         return -EINVAL;
676     }
677 
678     if (&pDwmModule->config != pConfig) {
679         memcpy(&pDwmModule->config, pConfig, sizeof(effect_config_t));
680     }
681 
682     if (init) {
683         pDownmixer->type = DOWNMIX_TYPE_FOLD;
684         pDownmixer->apply_volume_correction = false;
685         pDownmixer->input_channel_count = 8; // matches default input of AUDIO_CHANNEL_OUT_7POINT1
686     } else {
687         // when configuring the effect, do not allow a blank or unsupported channel mask
688         if (!Downmix_validChannelMask(pConfig->inputCfg.channels)) {
689             ALOGE("Downmix_Configure error: input channel mask(0x%x) not supported",
690                                                         pConfig->inputCfg.channels);
691             return -EINVAL;
692         }
693         pDownmixer->input_channel_count =
694                 audio_channel_count_from_out_mask(pConfig->inputCfg.channels);
695     }
696 
697     Downmix_Reset(pDownmixer, init);
698 
699     return 0;
700 }
701 
702 
703 /*----------------------------------------------------------------------------
704  * Downmix_Reset()
705  *----------------------------------------------------------------------------
706  * Purpose:
707  *  Reset internal states.
708  *
709  * Inputs:
710  *  pDownmixer   pointer to downmix context
711  *  init         true if called from init function
712  *
713  * Outputs:
714 *
715  * Returns:
716  *  0            indicates success
717  *
718  * Side Effects:
719  *
720  *----------------------------------------------------------------------------
721  */
722 
Downmix_Reset(downmix_object_t *,bool)723 static int Downmix_Reset(downmix_object_t* /* pDownmixer */, bool /* init */) {
724     // nothing to do here
725     return 0;
726 }
727 
728 
729 /*----------------------------------------------------------------------------
730  * Downmix_setParameter()
731  *----------------------------------------------------------------------------
732  * Purpose:
733  * Set a Downmix parameter
734  *
735  * Inputs:
736  *  pDownmixer    handle to instance data
737  *  param         parameter
738  *  pValue        pointer to parameter value
739  *  size          value size
740  *
741  * Outputs:
742  *
743  * Returns:
744  *  0             indicates success
745  *
746  * Side Effects:
747  *
748  *----------------------------------------------------------------------------
749  */
Downmix_setParameter(downmix_object_t * pDownmixer,int32_t param,uint32_t size,void * pValue)750 static int Downmix_setParameter(
751         downmix_object_t *pDownmixer, int32_t param, uint32_t size, void *pValue) {
752 
753     int16_t value16;
754     ALOGV("Downmix_setParameter, context %p, param %d, value16 %d, value32 %d",
755             pDownmixer, param, *(int16_t *)pValue, *(int32_t *)pValue);
756 
757     switch (param) {
758 
759       case DOWNMIX_PARAM_TYPE:
760         if (size != sizeof(downmix_type_t)) {
761             ALOGE("Downmix_setParameter(DOWNMIX_PARAM_TYPE) invalid size %u, should be %zu",
762                     size, sizeof(downmix_type_t));
763             return -EINVAL;
764         }
765         value16 = *(int16_t *)pValue;
766         ALOGV("set DOWNMIX_PARAM_TYPE, type %d", value16);
767         if (!((value16 > DOWNMIX_TYPE_INVALID) && (value16 <= DOWNMIX_TYPE_LAST))) {
768             ALOGE("Downmix_setParameter invalid DOWNMIX_PARAM_TYPE value %d", value16);
769             return -EINVAL;
770         } else {
771             pDownmixer->type = (downmix_type_t) value16;
772         break;
773 
774       default:
775         ALOGE("Downmix_setParameter unknown parameter %d", param);
776         return -EINVAL;
777     }
778 }
779 
780     return 0;
781 } /* end Downmix_setParameter */
782 
783 
784 /*----------------------------------------------------------------------------
785  * Downmix_getParameter()
786  *----------------------------------------------------------------------------
787  * Purpose:
788  * Get a Downmix parameter
789  *
790  * Inputs:
791  *  pDownmixer    handle to instance data
792  *  param         parameter
793  *  pValue        pointer to variable to hold retrieved value
794  *  pSize         pointer to value size: maximum size as input
795  *
796  * Outputs:
797  *  *pValue updated with parameter value
798  *  *pSize updated with actual value size
799  *
800  * Returns:
801  *  0             indicates success
802  *
803  * Side Effects:
804  *
805  *----------------------------------------------------------------------------
806  */
Downmix_getParameter(downmix_object_t * pDownmixer,int32_t param,uint32_t * pSize,void * pValue)807 static int Downmix_getParameter(
808         downmix_object_t *pDownmixer, int32_t param, uint32_t *pSize, void *pValue) {
809     int16_t *pValue16;
810 
811     switch (param) {
812 
813     case DOWNMIX_PARAM_TYPE:
814       if (*pSize < sizeof(int16_t)) {
815           ALOGE("Downmix_getParameter invalid parameter size %u for DOWNMIX_PARAM_TYPE", *pSize);
816           return -EINVAL;
817       }
818       pValue16 = (int16_t *)pValue;
819       *pValue16 = (int16_t) pDownmixer->type;
820       *pSize = sizeof(int16_t);
821       ALOGV("Downmix_getParameter DOWNMIX_PARAM_TYPE is %d", *pValue16);
822       break;
823 
824     default:
825       ALOGE("Downmix_getParameter unknown parameter %d", param);
826       return -EINVAL;
827     }
828 
829     return 0;
830 } /* end Downmix_getParameter */
831 
832 
833 /*----------------------------------------------------------------------------
834  * Downmix_foldFromQuad()
835  *----------------------------------------------------------------------------
836  * Purpose:
837  * downmix a quad signal to stereo
838  *
839  * Inputs:
840  *  pSrc       quad audio samples to downmix
841  *  numFrames  the number of quad frames to downmix
842  *  accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
843  *               or overwrite pDst (when false)
844  *
845  * Outputs:
846  *  pDst       downmixed stereo audio samples
847  *
848  *----------------------------------------------------------------------------
849  */
Downmix_foldFromQuad(float * pSrc,float * pDst,size_t numFrames,bool accumulate)850 void Downmix_foldFromQuad(float *pSrc, float *pDst, size_t numFrames, bool accumulate) {
851     // sample at index 0 is FL
852     // sample at index 1 is FR
853     // sample at index 2 is RL
854     // sample at index 3 is RR
855     if (accumulate) {
856         while (numFrames) {
857             // FL + RL
858             pDst[0] = clamp_float(pDst[0] + ((pSrc[0] + pSrc[2]) / 2.0f));
859             // FR + RR
860             pDst[1] = clamp_float(pDst[1] + ((pSrc[1] + pSrc[3]) / 2.0f));
861             pSrc += 4;
862             pDst += 2;
863             numFrames--;
864         }
865     } else { // same code as above but without adding and clamping pDst[i] to itself
866         while (numFrames) {
867             // FL + RL
868             pDst[0] = clamp_float((pSrc[0] + pSrc[2]) / 2.0f);
869             // FR + RR
870             pDst[1] = clamp_float((pSrc[1] + pSrc[3]) / 2.0f);
871             pSrc += 4;
872             pDst += 2;
873             numFrames--;
874         }
875     }
876 }
877 
878 /*----------------------------------------------------------------------------
879  * Downmix_foldFrom5Point1()
880  *----------------------------------------------------------------------------
881  * Purpose:
882  * downmix a 5.1 signal to stereo
883  *
884  * Inputs:
885  *  pSrc       5.1 audio samples to downmix
886  *  numFrames  the number of 5.1 frames to downmix
887  *  accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
888  *               or overwrite pDst (when false)
889  *
890  * Outputs:
891  *  pDst       downmixed stereo audio samples
892  *
893  *----------------------------------------------------------------------------
894  */
Downmix_foldFrom5Point1(float * pSrc,float * pDst,size_t numFrames,bool accumulate)895 void Downmix_foldFrom5Point1(float *pSrc, float *pDst, size_t numFrames, bool accumulate) {
896     float lt, rt, centerPlusLfeContrib; // samples in Q19.12 format
897     // sample at index 0 is FL
898     // sample at index 1 is FR
899     // sample at index 2 is FC
900     // sample at index 3 is LFE
901     // sample at index 4 is RL
902     // sample at index 5 is RR
903     // code is mostly duplicated between the two values of accumulate to avoid repeating the test
904     // for every sample
905     if (accumulate) {
906         while (numFrames) {
907             // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
908             centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
909                     + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
910             // FL + centerPlusLfeContrib + RL
911             lt = pSrc[0] + centerPlusLfeContrib + pSrc[4];
912             // FR + centerPlusLfeContrib + RR
913             rt = pSrc[1] + centerPlusLfeContrib + pSrc[5];
914             // accumulate in destination
915             pDst[0] = clamp_float(pDst[0] + (lt / 2.0f));
916             pDst[1] = clamp_float(pDst[1] + (rt / 2.0f));
917             pSrc += 6;
918             pDst += 2;
919             numFrames--;
920         }
921     } else { // same code as above but without adding and clamping pDst[i] to itself
922         while (numFrames) {
923             // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
924             centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
925                     + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
926             // FL + centerPlusLfeContrib + RL
927             lt = pSrc[0] + centerPlusLfeContrib + pSrc[4];
928             // FR + centerPlusLfeContrib + RR
929             rt = pSrc[1] + centerPlusLfeContrib + pSrc[5];
930             // store in destination
931             pDst[0] = clamp_float(lt / 2.0f); // differs from when accumulate is true above
932             pDst[1] = clamp_float(rt / 2.0f); // differs from when accumulate is true above
933             pSrc += 6;
934             pDst += 2;
935             numFrames--;
936         }
937     }
938 }
939 
940 /*----------------------------------------------------------------------------
941  * Downmix_foldFrom7Point1()
942  *----------------------------------------------------------------------------
943  * Purpose:
944  * downmix a 7.1 signal to stereo
945  *
946  * Inputs:
947  *  pSrc       7.1 audio samples to downmix
948  *  numFrames  the number of 7.1 frames to downmix
949  *  accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
950  *               or overwrite pDst (when false)
951  *
952  * Outputs:
953  *  pDst       downmixed stereo audio samples
954  *
955  *----------------------------------------------------------------------------
956  */
Downmix_foldFrom7Point1(float * pSrc,float * pDst,size_t numFrames,bool accumulate)957 void Downmix_foldFrom7Point1(float *pSrc, float *pDst, size_t numFrames, bool accumulate) {
958     float lt, rt, centerPlusLfeContrib; // samples in Q19.12 format
959     // sample at index 0 is FL
960     // sample at index 1 is FR
961     // sample at index 2 is FC
962     // sample at index 3 is LFE
963     // sample at index 4 is RL
964     // sample at index 5 is RR
965     // sample at index 6 is SL
966     // sample at index 7 is SR
967     // code is mostly duplicated between the two values of accumulate to avoid repeating the test
968     // for every sample
969     if (accumulate) {
970         while (numFrames) {
971             // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
972             centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
973                     + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
974             // FL + centerPlusLfeContrib + SL + RL
975             lt = pSrc[0] + centerPlusLfeContrib + pSrc[6] + pSrc[4];
976             // FR + centerPlusLfeContrib + SR + RR
977             rt = pSrc[1] + centerPlusLfeContrib + pSrc[7] + pSrc[5];
978             //accumulate in destination
979             pDst[0] = clamp_float(pDst[0] + (lt / 2.0f));
980             pDst[1] = clamp_float(pDst[1] + (rt / 2.0f));
981             pSrc += 8;
982             pDst += 2;
983             numFrames--;
984         }
985     } else { // same code as above but without adding and clamping pDst[i] to itself
986         while (numFrames) {
987             // centerPlusLfeContrib = FC(-3dB) + LFE(-3dB)
988             centerPlusLfeContrib = (pSrc[2] * MINUS_3_DB_IN_FLOAT)
989                     + (pSrc[3] * MINUS_3_DB_IN_FLOAT);
990             // FL + centerPlusLfeContrib + SL + RL
991             lt = pSrc[0] + centerPlusLfeContrib + pSrc[6] + pSrc[4];
992             // FR + centerPlusLfeContrib + SR + RR
993             rt = pSrc[1] + centerPlusLfeContrib + pSrc[7] + pSrc[5];
994             // store in destination
995             pDst[0] = clamp_float(lt / 2.0f); // differs from when accumulate is true above
996             pDst[1] = clamp_float(rt / 2.0f); // differs from when accumulate is true above
997             pSrc += 8;
998             pDst += 2;
999             numFrames--;
1000         }
1001     }
1002 }
1003 
1004 /*----------------------------------------------------------------------------
1005  * Downmix_foldGeneric()
1006  *----------------------------------------------------------------------------
1007  * Purpose:
1008  * downmix to stereo a multichannel signal of arbitrary channel position mask.
1009  *
1010  * Inputs:
1011  *  mask       the channel mask of pSrc
1012  *  pSrc       multichannel audio buffer to downmix
1013  *  numFrames  the number of multichannel frames to downmix
1014  *  accumulate whether to mix (when true) the result of the downmix with the contents of pDst,
1015  *               or overwrite pDst (when false)
1016  *
1017  * Outputs:
1018  *  pDst       downmixed stereo audio samples
1019  *
1020  * Returns: false if multichannel format is not supported
1021  *
1022  *----------------------------------------------------------------------------
1023  */
Downmix_foldGeneric(uint32_t mask,float * pSrc,float * pDst,size_t numFrames,bool accumulate)1024 bool Downmix_foldGeneric(
1025         uint32_t mask, float *pSrc, float *pDst, size_t numFrames, bool accumulate) {
1026 
1027     if (!Downmix_validChannelMask(mask)) {
1028         return false;
1029     }
1030     const int numChan = audio_channel_count_from_out_mask(mask);
1031 
1032     // compute at what index each channel is: samples will be in the following order:
1033     //   FL  FR  FC    LFE   BL  BR  BC    SL  SR
1034     //
1035     //  (transfer matrix)
1036     //   FL  FR  FC    LFE   BL  BR  BC    SL  SR
1037     //   0.5     0.353 0.353 0.5     0.353 0.5
1038     //       0.5 0.353 0.353     0.5 0.353     0.5
1039 
1040     // derive the indices for the transfer matrix columns that have non-zero values.
1041     int indexFL = -1;
1042     int indexFR = -1;
1043     int indexFC = -1;
1044     int indexLFE = -1;
1045     int indexBL = -1;
1046     int indexBR = -1;
1047     int indexBC = -1;
1048     int indexSL = -1;
1049     int indexSR = -1;
1050     int index = 0;
1051     for (unsigned tmp = mask;
1052          (tmp & (AUDIO_CHANNEL_OUT_7POINT1 | AUDIO_CHANNEL_OUT_BACK_CENTER)) != 0;
1053          ++index) {
1054         const unsigned lowestBit = tmp & -(signed)tmp;
1055         switch (lowestBit) {
1056         case AUDIO_CHANNEL_OUT_FRONT_LEFT:
1057             indexFL = index;
1058             break;
1059         case AUDIO_CHANNEL_OUT_FRONT_RIGHT:
1060             indexFR = index;
1061             break;
1062         case AUDIO_CHANNEL_OUT_FRONT_CENTER:
1063             indexFC = index;
1064             break;
1065         case AUDIO_CHANNEL_OUT_LOW_FREQUENCY:
1066             indexLFE = index;
1067             break;
1068         case AUDIO_CHANNEL_OUT_BACK_LEFT:
1069             indexBL = index;
1070             break;
1071         case AUDIO_CHANNEL_OUT_BACK_RIGHT:
1072             indexBR = index;
1073             break;
1074         case AUDIO_CHANNEL_OUT_BACK_CENTER:
1075             indexBC = index;
1076             break;
1077         case AUDIO_CHANNEL_OUT_SIDE_LEFT:
1078             indexSL = index;
1079             break;
1080         case AUDIO_CHANNEL_OUT_SIDE_RIGHT:
1081             indexSR = index;
1082             break;
1083         }
1084         tmp ^= lowestBit;
1085     }
1086 
1087     // With good branch prediction, this should run reasonably fast.
1088     // Also consider using a transfer matrix form.
1089     while (numFrames) {
1090         // compute contribution of FC, BC and LFE
1091         float centersLfeContrib = 0;
1092         if (indexFC >= 0) centersLfeContrib = pSrc[indexFC];
1093         if (indexLFE >= 0) centersLfeContrib += pSrc[indexLFE];
1094         if (indexBC >= 0) centersLfeContrib += pSrc[indexBC];
1095         centersLfeContrib *= MINUS_3_DB_IN_FLOAT;
1096 
1097         float ch[2];
1098         ch[0] = centersLfeContrib;
1099         ch[1] = centersLfeContrib;
1100 
1101         // mix in left / right channels
1102         if (indexFL >= 0) ch[0] += pSrc[indexFL];
1103         if (indexFR >= 0) ch[1] += pSrc[indexFR];
1104 
1105         if (indexSL >= 0) ch[0] += pSrc[indexSL];
1106         if (indexSR >= 0) ch[1] += pSrc[indexSR]; // note pair checks enforce this if indexSL != 0
1107 
1108         if (indexBL >= 0) ch[0] += pSrc[indexBL];
1109         if (indexBR >= 0) ch[1] += pSrc[indexBR]; // note pair checks enforce this if indexBL != 0
1110 
1111         // scale to prevent overflow.
1112         ch[0] *= 0.5f;
1113         ch[1] *= 0.5f;
1114 
1115         if (accumulate) {
1116             ch[0] += pDst[0];
1117             ch[1] += pDst[1];
1118         }
1119 
1120         pDst[0] = clamp_float(ch[0]);
1121         pDst[1] = clamp_float(ch[1]);
1122         pSrc += numChan;
1123         pDst += 2;
1124         numFrames--;
1125     }
1126     return true;
1127 }
1128