1 /*
2  * Copyright (C) 2016 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 
18 #ifndef ANDROID_AUDIO_EFFECT_CORE_H
19 #define ANDROID_AUDIO_EFFECT_CORE_H
20 
21 #include "audio.h"
22 #include "audio_effect-base.h"
23 
24 __BEGIN_DECLS
25 
26 /////////////////////////////////////////////////
27 //      Common Definitions
28 /////////////////////////////////////////////////
29 
30 //
31 //--- Effect descriptor structure effect_descriptor_t
32 //
33 
34 // This format is used for both "type" and "uuid" fields of the effect descriptor structure.
35 // - When used for effect type and the engine is implementing and effect corresponding to a standard
36 // OpenSL ES interface, this ID must be the one defined in OpenSLES_IID.h for that interface.
37 // - When used as uuid, it should be a unique UUID for this particular implementation.
38 typedef audio_uuid_t effect_uuid_t;
39 
40 // Maximum length of character strings in structures defines by this API.
41 #define EFFECT_STRING_LEN_MAX 64
42 
43 // NULL UUID definition (matches SL_IID_NULL_)
44 #define EFFECT_UUID_INITIALIZER { 0xec7178ec, 0xe5e1, 0x4432, 0xa3f4, \
45                                   { 0x46, 0x57, 0xe6, 0x79, 0x52, 0x10 } }
46 static const effect_uuid_t EFFECT_UUID_NULL_ = EFFECT_UUID_INITIALIZER;
47 static const effect_uuid_t * const EFFECT_UUID_NULL = &EFFECT_UUID_NULL_;
48 static const char * const EFFECT_UUID_NULL_STR = "ec7178ec-e5e1-4432-a3f4-4657e6795210";
49 
50 // The effect descriptor contains necessary information to facilitate the enumeration of the effect
51 // engines present in a library.
52 typedef struct effect_descriptor_s {
53     effect_uuid_t type;     // UUID of to the OpenSL ES interface implemented by this effect
54     effect_uuid_t uuid;     // UUID for this particular implementation
55     uint32_t apiVersion;    // Version of the effect control API implemented
56     uint32_t flags;         // effect engine capabilities/requirements flags (see below)
57     uint16_t cpuLoad;       // CPU load indication (see below)
58     uint16_t memoryUsage;   // Data Memory usage (see below)
59     char    name[EFFECT_STRING_LEN_MAX];   // human readable effect name
60     char    implementor[EFFECT_STRING_LEN_MAX];    // human readable effect implementor name
61 } effect_descriptor_t;
62 
63 #define EFFECT_CONFIG_ALL (EFFECT_CONFIG_BUFFER | \
64                            EFFECT_CONFIG_SMP_RATE | \
65                            EFFECT_CONFIG_CHANNELS | \
66                            EFFECT_CONFIG_FORMAT | \
67                            EFFECT_CONFIG_ACC_MODE)
68 
69 /////////////////////////////////////////////////
70 //      Effect control interface
71 /////////////////////////////////////////////////
72 
73 //
74 //--- Standardized command codes for command() function
75 //
76 enum effect_command_e {
77    EFFECT_CMD_INIT,                 // initialize effect engine
78    EFFECT_CMD_SET_CONFIG,           // configure effect engine (see effect_config_t)
79    EFFECT_CMD_RESET,                // reset effect engine
80    EFFECT_CMD_ENABLE,               // enable effect process
81    EFFECT_CMD_DISABLE,              // disable effect process
82    EFFECT_CMD_SET_PARAM,            // set parameter immediately (see effect_param_t)
83    EFFECT_CMD_SET_PARAM_DEFERRED,   // set parameter deferred
84    EFFECT_CMD_SET_PARAM_COMMIT,     // commit previous set parameter deferred
85    EFFECT_CMD_GET_PARAM,            // get parameter
86    EFFECT_CMD_SET_DEVICE,           // set audio device (see audio.h, audio_devices_t)
87    EFFECT_CMD_SET_VOLUME,           // set volume
88    EFFECT_CMD_SET_AUDIO_MODE,       // set the audio mode (normal, ring, ...)
89    EFFECT_CMD_SET_CONFIG_REVERSE,   // configure effect engine reverse stream(see effect_config_t)
90    EFFECT_CMD_SET_INPUT_DEVICE,     // set capture device (see audio.h, audio_devices_t)
91    EFFECT_CMD_GET_CONFIG,           // read effect engine configuration
92    EFFECT_CMD_GET_CONFIG_REVERSE,   // read configure effect engine reverse stream configuration
93    EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS,// get all supported configurations for a feature.
94    EFFECT_CMD_GET_FEATURE_CONFIG,   // get current feature configuration
95    EFFECT_CMD_SET_FEATURE_CONFIG,   // set current feature configuration
96    EFFECT_CMD_SET_AUDIO_SOURCE,     // set the audio source (see audio.h, audio_source_t)
97    EFFECT_CMD_OFFLOAD,              // set if effect thread is an offload one,
98                                     // send the ioHandle of the effect thread
99    EFFECT_CMD_DUMP,                 // dump effect current state, for debugging
100    EFFECT_CMD_FIRST_PROPRIETARY = 0x10000 // first proprietary command code
101 };
102 
103 //==================================================================================================
104 // command: EFFECT_CMD_INIT
105 //--------------------------------------------------------------------------------------------------
106 // description:
107 //  Initialize effect engine: All configurations return to default
108 //--------------------------------------------------------------------------------------------------
109 // command format:
110 //  size: 0
111 //  data: N/A
112 //--------------------------------------------------------------------------------------------------
113 // reply format:
114 //  size: sizeof(int)
115 //  data: status
116 //==================================================================================================
117 // command: EFFECT_CMD_SET_CONFIG
118 //--------------------------------------------------------------------------------------------------
119 // description:
120 //  Apply new audio parameters configurations for input and output buffers
121 //--------------------------------------------------------------------------------------------------
122 // command format:
123 //  size: sizeof(effect_config_t)
124 //  data: effect_config_t
125 //--------------------------------------------------------------------------------------------------
126 // reply format:
127 //  size: sizeof(int)
128 //  data: status
129 //==================================================================================================
130 // command: EFFECT_CMD_RESET
131 //--------------------------------------------------------------------------------------------------
132 // description:
133 //  Reset the effect engine. Keep configuration but resets state and buffer content
134 //--------------------------------------------------------------------------------------------------
135 // command format:
136 //  size: 0
137 //  data: N/A
138 //--------------------------------------------------------------------------------------------------
139 // reply format:
140 //  size: 0
141 //  data: N/A
142 //==================================================================================================
143 // command: EFFECT_CMD_ENABLE
144 //--------------------------------------------------------------------------------------------------
145 // description:
146 //  Enable the process. Called by the framework before the first call to process()
147 //--------------------------------------------------------------------------------------------------
148 // command format:
149 //  size: 0
150 //  data: N/A
151 //--------------------------------------------------------------------------------------------------
152 // reply format:
153 //  size: sizeof(int)
154 //  data: status
155 //==================================================================================================
156 // command: EFFECT_CMD_DISABLE
157 //--------------------------------------------------------------------------------------------------
158 // description:
159 //  Disable the process. Called by the framework after the last call to process()
160 //--------------------------------------------------------------------------------------------------
161 // command format:
162 //  size: 0
163 //  data: N/A
164 //--------------------------------------------------------------------------------------------------
165 // reply format:
166 //  size: sizeof(int)
167 //  data: status
168 //==================================================================================================
169 // command: EFFECT_CMD_SET_PARAM
170 //--------------------------------------------------------------------------------------------------
171 // description:
172 //  Set a parameter and apply it immediately
173 //--------------------------------------------------------------------------------------------------
174 // command format:
175 //  size: sizeof(effect_param_t) + size of param and value
176 //  data: effect_param_t + param + value. See effect_param_t definition below for value offset
177 //--------------------------------------------------------------------------------------------------
178 // reply format:
179 //  size: sizeof(int)
180 //  data: status
181 //==================================================================================================
182 // command: EFFECT_CMD_SET_PARAM_DEFERRED
183 //--------------------------------------------------------------------------------------------------
184 // description:
185 //  Set a parameter but apply it only when receiving EFFECT_CMD_SET_PARAM_COMMIT command
186 //--------------------------------------------------------------------------------------------------
187 // command format:
188 //  size: sizeof(effect_param_t) + size of param and value
189 //  data: effect_param_t + param + value. See effect_param_t definition below for value offset
190 //--------------------------------------------------------------------------------------------------
191 // reply format:
192 //  size: 0
193 //  data: N/A
194 //==================================================================================================
195 // command: EFFECT_CMD_SET_PARAM_COMMIT
196 //--------------------------------------------------------------------------------------------------
197 // description:
198 //  Apply all previously received EFFECT_CMD_SET_PARAM_DEFERRED commands
199 //--------------------------------------------------------------------------------------------------
200 // command format:
201 //  size: 0
202 //  data: N/A
203 //--------------------------------------------------------------------------------------------------
204 // reply format:
205 //  size: sizeof(int)
206 //  data: status
207 //==================================================================================================
208 // command: EFFECT_CMD_GET_PARAM
209 //--------------------------------------------------------------------------------------------------
210 // description:
211 //  Get a parameter value
212 //--------------------------------------------------------------------------------------------------
213 // command format:
214 //  size: sizeof(effect_param_t) + size of param
215 //  data: effect_param_t + param
216 //--------------------------------------------------------------------------------------------------
217 // reply format:
218 //  size: sizeof(effect_param_t) + size of param and value
219 //  data: effect_param_t + param + value. See effect_param_t definition below for value offset
220 //==================================================================================================
221 // command: EFFECT_CMD_SET_DEVICE
222 //--------------------------------------------------------------------------------------------------
223 // description:
224 //  Set the rendering device the audio output path is connected to. See audio.h, audio_devices_t
225 //  for device values.
226 //  The effect implementation must set EFFECT_FLAG_DEVICE_IND flag in its descriptor to receive this
227 //  command when the device changes
228 //--------------------------------------------------------------------------------------------------
229 // command format:
230 //  size: sizeof(uint32_t)
231 //  data: uint32_t
232 //--------------------------------------------------------------------------------------------------
233 // reply format:
234 //  size: 0
235 //  data: N/A
236 //==================================================================================================
237 // command: EFFECT_CMD_SET_VOLUME
238 //--------------------------------------------------------------------------------------------------
239 // description:
240 //  Set and get volume. Used by audio framework to delegate volume control to effect engine.
241 //  The effect implementation must set EFFECT_FLAG_VOLUME_IND or EFFECT_FLAG_VOLUME_CTRL flag in
242 //  its descriptor to receive this command before every call to process() function
243 //  If EFFECT_FLAG_VOLUME_CTRL flag is set in the effect descriptor, the effect engine must return
244 //  the volume that should be applied before the effect is processed. The overall volume (the volume
245 //  actually applied by the effect engine multiplied by the returned value) should match the value
246 //  indicated in the command.
247 //--------------------------------------------------------------------------------------------------
248 // command format:
249 //  size: n * sizeof(uint32_t)
250 //  data: volume for each channel defined in effect_config_t for output buffer expressed in
251 //      8.24 fixed point format
252 //--------------------------------------------------------------------------------------------------
253 // reply format:
254 //  size: n * sizeof(uint32_t) / 0
255 //  data: - if EFFECT_FLAG_VOLUME_CTRL is set in effect descriptor:
256 //              volume for each channel defined in effect_config_t for output buffer expressed in
257 //              8.24 fixed point format
258 //        - if EFFECT_FLAG_VOLUME_CTRL is not set in effect descriptor:
259 //              N/A
260 //  It is legal to receive a null pointer as pReplyData in which case the effect framework has
261 //  delegated volume control to another effect
262 //==================================================================================================
263 // command: EFFECT_CMD_SET_AUDIO_MODE
264 //--------------------------------------------------------------------------------------------------
265 // description:
266 //  Set the audio mode. The effect implementation must set EFFECT_FLAG_AUDIO_MODE_IND flag in its
267 //  descriptor to receive this command when the audio mode changes.
268 //--------------------------------------------------------------------------------------------------
269 // command format:
270 //  size: sizeof(uint32_t)
271 //  data: audio_mode_t
272 //--------------------------------------------------------------------------------------------------
273 // reply format:
274 //  size: 0
275 //  data: N/A
276 //==================================================================================================
277 // command: EFFECT_CMD_SET_CONFIG_REVERSE
278 //--------------------------------------------------------------------------------------------------
279 // description:
280 //  Apply new audio parameters configurations for input and output buffers of reverse stream.
281 //  An example of reverse stream is the echo reference supplied to an Acoustic Echo Canceler.
282 //--------------------------------------------------------------------------------------------------
283 // command format:
284 //  size: sizeof(effect_config_t)
285 //  data: effect_config_t
286 //--------------------------------------------------------------------------------------------------
287 // reply format:
288 //  size: sizeof(int)
289 //  data: status
290 //==================================================================================================
291 // command: EFFECT_CMD_SET_INPUT_DEVICE
292 //--------------------------------------------------------------------------------------------------
293 // description:
294 //  Set the capture device the audio input path is connected to. See audio.h, audio_devices_t
295 //  for device values.
296 //  The effect implementation must set EFFECT_FLAG_DEVICE_IND flag in its descriptor to receive this
297 //  command when the device changes
298 //--------------------------------------------------------------------------------------------------
299 // command format:
300 //  size: sizeof(uint32_t)
301 //  data: uint32_t
302 //--------------------------------------------------------------------------------------------------
303 // reply format:
304 //  size: 0
305 //  data: N/A
306 //==================================================================================================
307 // command: EFFECT_CMD_GET_CONFIG
308 //--------------------------------------------------------------------------------------------------
309 // description:
310 //  Read audio parameters configurations for input and output buffers
311 //--------------------------------------------------------------------------------------------------
312 // command format:
313 //  size: 0
314 //  data: N/A
315 //--------------------------------------------------------------------------------------------------
316 // reply format:
317 //  size: sizeof(effect_config_t)
318 //  data: effect_config_t
319 //==================================================================================================
320 // command: EFFECT_CMD_GET_CONFIG_REVERSE
321 //--------------------------------------------------------------------------------------------------
322 // description:
323 //  Read audio parameters configurations for input and output buffers of reverse stream
324 //--------------------------------------------------------------------------------------------------
325 // command format:
326 //  size: 0
327 //  data: N/A
328 //--------------------------------------------------------------------------------------------------
329 // reply format:
330 //  size: sizeof(effect_config_t)
331 //  data: effect_config_t
332 //==================================================================================================
333 // command: EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS
334 //--------------------------------------------------------------------------------------------------
335 // description:
336 //  Queries for supported configurations for a particular feature (e.g. get the supported
337 // combinations of main and auxiliary channels for a noise suppressor).
338 // The command parameter is the feature identifier (See effect_feature_e for a list of defined
339 // features) followed by the maximum number of configuration descriptor to return.
340 // The reply is composed of:
341 //  - status (uint32_t):
342 //          - 0 if feature is supported
343 //          - -ENOSYS if the feature is not supported,
344 //          - -ENOMEM if the feature is supported but the total number of supported configurations
345 //          exceeds the maximum number indicated by the caller.
346 //  - total number of supported configurations (uint32_t)
347 //  - an array of configuration descriptors.
348 // The actual number of descriptors returned must not exceed the maximum number indicated by
349 // the caller.
350 //--------------------------------------------------------------------------------------------------
351 // command format:
352 //  size: 2 x sizeof(uint32_t)
353 //  data: effect_feature_e + maximum number of configurations to return
354 //--------------------------------------------------------------------------------------------------
355 // reply format:
356 //  size: 2 x sizeof(uint32_t) + n x sizeof (<config descriptor>)
357 //  data: status + total number of configurations supported + array of n config descriptors
358 //==================================================================================================
359 // command: EFFECT_CMD_GET_FEATURE_CONFIG
360 //--------------------------------------------------------------------------------------------------
361 // description:
362 //  Retrieves current configuration for a given feature.
363 // The reply status is:
364 //      - 0 if feature is supported
365 //      - -ENOSYS if the feature is not supported,
366 //--------------------------------------------------------------------------------------------------
367 // command format:
368 //  size: sizeof(uint32_t)
369 //  data: effect_feature_e
370 //--------------------------------------------------------------------------------------------------
371 // reply format:
372 //  size: sizeof(uint32_t) + sizeof (<config descriptor>)
373 //  data: status + config descriptor
374 //==================================================================================================
375 // command: EFFECT_CMD_SET_FEATURE_CONFIG
376 //--------------------------------------------------------------------------------------------------
377 // description:
378 //  Sets current configuration for a given feature.
379 // The reply status is:
380 //      - 0 if feature is supported
381 //      - -ENOSYS if the feature is not supported,
382 //      - -EINVAL if the configuration is invalid
383 //--------------------------------------------------------------------------------------------------
384 // command format:
385 //  size: sizeof(uint32_t) + sizeof (<config descriptor>)
386 //  data: effect_feature_e + config descriptor
387 //--------------------------------------------------------------------------------------------------
388 // reply format:
389 //  size: sizeof(uint32_t)
390 //  data: status
391 //==================================================================================================
392 // command: EFFECT_CMD_SET_AUDIO_SOURCE
393 //--------------------------------------------------------------------------------------------------
394 // description:
395 //  Set the audio source the capture path is configured for (Camcorder, voice recognition...).
396 //  See audio.h, audio_source_t for values.
397 //--------------------------------------------------------------------------------------------------
398 // command format:
399 //  size: sizeof(uint32_t)
400 //  data: uint32_t
401 //--------------------------------------------------------------------------------------------------
402 // reply format:
403 //  size: 0
404 //  data: N/A
405 //==================================================================================================
406 // command: EFFECT_CMD_OFFLOAD
407 //--------------------------------------------------------------------------------------------------
408 // description:
409 //  1.indicate if the playback thread the effect is attached to is offloaded or not
410 //  2.update the io handle of the playback thread the effect is attached to
411 //--------------------------------------------------------------------------------------------------
412 // command format:
413 //  size: sizeof(effect_offload_param_t)
414 //  data: effect_offload_param_t
415 //--------------------------------------------------------------------------------------------------
416 // reply format:
417 //  size: sizeof(uint32_t)
418 //  data: uint32_t
419 //==================================================================================================
420 // command: EFFECT_CMD_DUMP
421 //--------------------------------------------------------------------------------------------------
422 // description:
423 //  Output the current state description into the provided file descriptor for inclusion
424 //  into the audio service dump
425 //--------------------------------------------------------------------------------------------------
426 // command format:
427 //  size: sizeof(uint32_t)
428 //  data: uint32_t (which is in fact a file descriptor, int)
429 //--------------------------------------------------------------------------------------------------
430 // reply format:
431 //  size: 0
432 //  data: N/A
433 //--------------------------------------------------------------------------------------------------
434 // command: EFFECT_CMD_FIRST_PROPRIETARY
435 //--------------------------------------------------------------------------------------------------
436 // description:
437 //  All proprietary effect commands must use command codes above this value. The size and format of
438 //  command and response fields is free in this case
439 //==================================================================================================
440 
441 // Audio buffer descriptor used by process(), bufferProvider() functions and buffer_config_t
442 // structure. Multi-channel audio is always interleaved. The channel order is from LSB to MSB with
443 // regard to the channel mask definition in audio.h, audio_channel_mask_t e.g :
444 // Stereo: left, right
445 // 5 point 1: front left, front right, front center, low frequency, back left, back right
446 // The buffer size is expressed in frame count, a frame being composed of samples for all
447 // channels at a given time. Frame size for unspecified format (AUDIO_FORMAT_OTHER) is 8 bit by
448 // definition
449 typedef struct audio_buffer_s {
450     size_t   frameCount;        // number of frames in buffer
451     union {
452         void*       raw;        // raw pointer to start of buffer
453         float*      f32;        // pointer to float 32 bit data at start of buffer
454         int32_t*    s32;        // pointer to signed 32 bit data at start of buffer
455         int16_t*    s16;        // pointer to signed 16 bit data at start of buffer
456         uint8_t*    u8;         // pointer to unsigned 8 bit data at start of buffer
457     };
458 } audio_buffer_t;
459 
460 // The buffer_provider_s structure contains functions that can be used
461 // by the effect engine process() function to query and release input
462 // or output audio buffer.
463 // The getBuffer() function is called to retrieve a buffer where data
464 // should read from or written to by process() function.
465 // The releaseBuffer() function MUST be called when the buffer retrieved
466 // with getBuffer() is not needed anymore.
467 // The process function should use the buffer provider mechanism to retrieve
468 // input or output buffer if the inBuffer or outBuffer passed as argument is NULL
469 // and the buffer configuration (buffer_config_t) given by the EFFECT_CMD_SET_CONFIG
470 // command did not specify an audio buffer.
471 
472 typedef int32_t (* buffer_function_t)(void *cookie, audio_buffer_t *buffer);
473 
474 typedef struct buffer_provider_s {
475     buffer_function_t getBuffer;       // retrieve next buffer
476     buffer_function_t releaseBuffer;   // release used buffer
477     void       *cookie;                // for use by client of buffer provider functions
478 } buffer_provider_t;
479 
480 // The buffer_config_s structure specifies the input or output audio format
481 // to be used by the effect engine.
482 typedef struct buffer_config_s {
483     audio_buffer_t  buffer;     // buffer for use by process() function if not passed explicitly
484     uint32_t   samplingRate;    // sampling rate
485     uint32_t   channels;        // channel mask (see audio_channel_mask_t in audio.h)
486     buffer_provider_t bufferProvider;   // buffer provider
487     uint8_t    format;          // Audio format (see audio_format_t in audio.h)
488     uint8_t    accessMode;      // read/write or accumulate in buffer (effect_buffer_access_e)
489     uint16_t   mask;            // indicates which of the above fields is valid
490 } buffer_config_t;
491 
492 // EFFECT_FEATURE_AUX_CHANNELS feature configuration descriptor. Describe a combination
493 // of main and auxiliary channels supported
494 typedef struct channel_config_s {
495     audio_channel_mask_t main_channels; // channel mask for main channels
496     audio_channel_mask_t aux_channels;  // channel mask for auxiliary channels
497 } channel_config_t;
498 
499 
500 // effect_config_s structure is used to configure audio parameters and buffers for effect engine
501 // input and output.
502 typedef struct effect_config_s {
503     buffer_config_t   inputCfg;
504     buffer_config_t   outputCfg;
505 } effect_config_t;
506 
507 
508 // effect_param_s structure describes the format of the pCmdData argument of EFFECT_CMD_SET_PARAM
509 // command and pCmdData and pReplyData of EFFECT_CMD_GET_PARAM command.
510 // psize and vsize represent the actual size of parameter and value.
511 //
512 // NOTE: the start of value field inside the data field is always on a 32 bit boundary:
513 //
514 //  +-----------+
515 //  | status    | sizeof(int)
516 //  +-----------+
517 //  | psize     | sizeof(int)
518 //  +-----------+
519 //  | vsize     | sizeof(int)
520 //  +-----------+
521 //  |           |   |           |
522 //  ~ parameter ~   > psize     |
523 //  |           |   |           >  ((psize - 1)/sizeof(int) + 1) * sizeof(int)
524 //  +-----------+               |
525 //  | padding   |               |
526 //  +-----------+
527 //  |           |   |
528 //  ~ value     ~   > vsize
529 //  |           |   |
530 //  +-----------+
531 
532 typedef struct effect_param_s {
533     int32_t     status;     // Transaction status (unused for command, used for reply)
534     uint32_t    psize;      // Parameter size
535     uint32_t    vsize;      // Value size
536     char        data[];     // Start of Parameter + Value data
537 } effect_param_t;
538 
539 // Maximum effect_param_t size
540 #define EFFECT_PARAM_SIZE_MAX       65536
541 
542 // structure used by EFFECT_CMD_OFFLOAD command
543 typedef struct effect_offload_param_s {
544     bool isOffload;         // true if the playback thread the effect is attached to is offloaded
545     int ioHandle;           // io handle of the playback thread the effect is attached to
546 } effect_offload_param_t;
547 
548 
549 __END_DECLS
550 
551 #endif  // ANDROID_AUDIO_EFFECT_CORE_H
552