summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/plugins/cadence/mixer/xa-mixer.c
blob: 0909d446316918316262afeeb4e69bcf81622c89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

/*******************************************************************************
 * xa-mixer.c
 *
 * Sample mixer plugin
 *
 ******************************************************************************/

#define MODULE_TAG                      MIXER

/*******************************************************************************
 * Includes
 ******************************************************************************/

#include "xf-plugin.h"
#include "audio/xa-mixer-api.h"

/*******************************************************************************
 * Tracing configuration
 ******************************************************************************/

TRACE_TAG(INIT, 1);
TRACE_TAG(PROCESS, 1);

/*******************************************************************************
 * Internal functions definitions
 ******************************************************************************/

/* ...API structure */
typedef struct XAPcmMixer
{
    /* ...mixer state */
    u32                 state;
    
    /* ...number of samples in a frame */
    u32                 frame_size;
    
    /* ...number of channels (channel mask?) */
    u32                 channels;
    
    /* ...PCM sample width */
    u32                 pcm_width;

    /* ...sampling rate */
    u32                 sample_rate;

    /* ...number of bytes in input/output buffer */
    u32                 buffer_size;
    
    /* ...master volume and individual track volumes*/
    u32                 volume[XA_MIXER_MAX_TRACK_NUMBER + 1];
    
    /* ...input buffers */
    void               *input[XA_MIXER_MAX_TRACK_NUMBER];
    
    /* ...number of samples in individual buffers */
    u32                 input_length[XA_MIXER_MAX_TRACK_NUMBER];

    /* ...output buffer */
    void               *output;

    /* ...number of produced bytes - do I need that? have buffer-size already - tbd */
    u32                 produced;
    
    /* ...scratch buffer pointer */
    void               *scratch;
    
}   XAPcmMixer;

/*******************************************************************************
 * Mixer state flags
 ******************************************************************************/

#define XA_MIXER_FLAG_PREINIT_DONE      (1 << 0)
#define XA_MIXER_FLAG_POSTINIT_DONE     (1 << 1)
#define XA_MIXER_FLAG_RUNNING           (1 << 2)
#define XA_MIXER_FLAG_OUTPUT            (1 << 3)
#define XA_MIXER_FLAG_COMPLETE          (1 << 4)

/*******************************************************************************
 * DSP functions
 ******************************************************************************/

#define DSP_SATURATE_S16(s32)   \
    (s16)((s32) > 0x7fff ? 0x7fff : ((s32) < -0x8000 ? -0x8000 : (s32)))

/* ...mixer preinitialization (default parameters) */
static inline void xa_mixer_preinit(XAPcmMixer *d)
{
    u32     i;
    
    /* ...pre-configuration initialization; reset internal data */
    memset(d, 0, sizeof(*d));
        
    /* ...set default parameters */
    d->pcm_width = 16, d->channels = 2, d->frame_size = 512;

    /* ...set default volumes (last index is a master volume)*/
    for (i = 0; i <= XA_MIXER_MAX_TRACK_NUMBER; i++)
    {
        d->volume[i] = ((1 << 12) << 16) | (1 << 12);
    }
}

/* ...do mixing of stereo PCM-16 streams */
static XA_ERRORCODE xa_mixer_do_execute_stereo_16bit(XAPcmMixer *d)
{
    s16    *output = d->output;
    s16    *b[XA_MIXER_MAX_TRACK_NUMBER]; 
    u16     v_l[XA_MIXER_MAX_TRACK_NUMBER];
    u16     v_r[XA_MIXER_MAX_TRACK_NUMBER];
    u16     w_l, w_r;    
    u32     t32;
    u32     i, j;
    
    /* ...retrieve master volume - assume up to 24dB amplifying (4 bits) */
    t32 = d->volume[XA_MIXER_MAX_TRACK_NUMBER];
    w_l = (u16)(t32 & 0xFFFF), w_r = (u16)(t32 >> 16);
    
    /* ...prepare individual tracks */
    for (j = 0; j < XA_MIXER_MAX_TRACK_NUMBER; j++)
    {
        u32     n = d->input_length[j];
        
        /* ...check if we have input buffer available */
        if (n == 0)
        {
            /* ...output silence (multiply garbage in the scratch buffer by 0) */
            b[j] = d->scratch;
            v_l[j] = v_r[j] = 0;
        }
        else
        {
            s32     k = (s32)(d->buffer_size - n);
            
            /* ...put input buffer */
            XF_CHK_ERR(b[j] = d->input[j], XA_MIXER_EXEC_FATAL_INPUT);
            
            /* ...if length is not sufficient, pad buffer remainder */
            (k > 0 ? memset((void *)b[j] + n, 0x00, k) : 0);
            
            /* ...set individual track volume/balance */
            t32 = d->volume[j];
            v_l[j] = (u16)(t32 & 0xFFFF), v_r[j] = (u16)(t32 >> 16);
        }

        TRACE(PROCESS, _b("b[%u] = %p%s"), j, b[j], (n == 0 ? " - scratch" : ""));
    }

    /* ...process all tracks */
    for (i = 0; i < d->frame_size; i++)
    {
        s32     l32 = 0, r32 = 0;

        /* ...fill-in every channel in our map (unrolls loop here) */
        for (j = 0; j < XA_MIXER_MAX_TRACK_NUMBER; j++)
        {
            /* ...left channel processing (no saturation here yet) */
            l32 += *b[j]++ * v_l[j];

            /* ...right channel processing */
            r32 += *b[j]++ * v_r[j];
        }

        /* ...normalize (truncate towards -inf) and multiply by master volume */
        l32 = ((l32 >> 12) * w_l) >> 12;
        r32 = ((r32 >> 12) * w_r) >> 12;

        /* ...saturate and store in buffer */
        *output++ = DSP_SATURATE_S16(l32);
        *output++ = DSP_SATURATE_S16(r32);
    }

    /* ...save total number of produced bytes */
    d->produced = (u32)((void *)output - d->output);

    /* ...put flag saying we have output buffer */
    d->state |= XA_MIXER_FLAG_OUTPUT;
    
    TRACE(PROCESS, _b("produced: %u bytes (%u samples)"), d->produced, d->frame_size);
    
    /* ...reset input buffer length? */
    //memset(d->input_length, 0, sizeof(d->input_length));

    /* ...return success result code */
    return XA_NO_ERROR;
}

/* ...runtime reset */
static XA_ERRORCODE xa_mixer_do_runtime_init(XAPcmMixer *d)
{
    /* ...no special processing is needed here */
    return XA_NO_ERROR;
}

/*******************************************************************************
 * Commands processing
 ******************************************************************************/

/* ...codec API size query */
static XA_ERRORCODE xa_mixer_get_api_size(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...check parameters are sane */
    XF_CHK_ERR(pv_value, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...retrieve API structure size */
    *(WORD32 *)pv_value = sizeof(*d);
    
    return XA_NO_ERROR;
}

/* ...standard codec initialization routine */
static XA_ERRORCODE xa_mixer_init(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...sanity check - mixer must be valid */
    XF_CHK_ERR(d, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...process particular initialization type */
    switch (i_idx)
    {
    case XA_CMD_TYPE_INIT_API_PRE_CONFIG_PARAMS:
    {
        /* ...pre-configuration initialization; reset internal data */
        xa_mixer_preinit(d);

        /* ...and mark mixer has been created */
        d->state = XA_MIXER_FLAG_PREINIT_DONE;
        
        return XA_NO_ERROR;
    }
    
    case XA_CMD_TYPE_INIT_API_POST_CONFIG_PARAMS:
    {
        /* ...post-configuration initialization (all parameters are set) */
        XF_CHK_ERR(d->state & XA_MIXER_FLAG_PREINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);
    
        /* ...calculate input/output buffer size in bytes */
        d->buffer_size = d->channels * d->frame_size * (d->pcm_width == 16 ? sizeof(s16) : sizeof(s32));
        
        /* ...mark post-initialization is complete */
        d->state |= XA_MIXER_FLAG_POSTINIT_DONE;
        
        return XA_NO_ERROR;
    }
    
    case XA_CMD_TYPE_INIT_PROCESS:
    {
        /* ...kick run-time initialization process; make sure mixer is setup */
        XF_CHK_ERR(d->state & XA_MIXER_FLAG_POSTINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);

        /* ...enter into execution stage */
        d->state |= XA_MIXER_FLAG_RUNNING;
        
        return XA_NO_ERROR;
    }
    
    case XA_CMD_TYPE_INIT_DONE_QUERY:
    {
        /* ...check if initialization is done; make sure pointer is sane */
        XF_CHK_ERR(pv_value, XA_API_FATAL_INVALID_CMD_TYPE);
        
        /* ...put current status */
        *(WORD32 *)pv_value = (d->state & XA_MIXER_FLAG_RUNNING ? 1 : 0);

        return XA_NO_ERROR;
    }
    
    default:
        /* ...unrecognized command type */
        TRACE(ERROR, _x("Unrecognized command type: %X"), i_idx);
        return XA_API_FATAL_INVALID_CMD_TYPE;
    }
}

/* ...set mixer configuration parameter */
static XA_ERRORCODE xa_mixer_set_config_param(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    u32     i_value;
    
    /* ...sanity check - mixer pointer must be sane */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...pre-initialization must be completed, mixer must be idle */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_PREINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...get parameter value  */
    i_value = (u32) *(WORD32 *)pv_value;
    
    /* ...process individual configuration parameter */
    switch (i_idx)
    {
    case XA_MIXER_CONFIG_PARAM_PCM_WIDTH:
        /* ...check value is permitted (16 bits only) */
        XF_CHK_ERR(i_value == 16, XA_MIXER_CONFIG_NONFATAL_RANGE);
        d->pcm_width = (u32)i_value;
        return XA_NO_ERROR;

    case XA_MIXER_CONFIG_PARAM_CHANNELS:
        /* ...allow stereo only */
        XF_CHK_ERR(i_value == 2, XA_MIXER_CONFIG_NONFATAL_RANGE);
        d->channels = (u32)i_value;
        return XA_NO_ERROR;

    case XA_MIXER_CONFIG_PARAM_SAMPLE_RATE:
        /* ...set mixer sample rate */
        d->sample_rate = (u32)i_value;
        return XA_NO_ERROR;
        
    default:
        TRACE(ERROR, _x("Invalid parameter: %X"), i_idx);
        return XA_API_FATAL_INVALID_CMD_TYPE;
    }
}

/* ...retrieve configuration parameter */
static XA_ERRORCODE xa_mixer_get_config_param(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...sanity check - mixer must be initialized */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...make sure pre-initialization is completed */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_PREINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...process individual configuration parameter */
    switch (i_idx)
    {
    case XA_MIXER_CONFIG_PARAM_INPUT_TRACKS:
        /* ...return maximal number of input tracks supported */
        *(WORD32 *)pv_value = XA_MIXER_MAX_TRACK_NUMBER;
        return XA_NO_ERROR;
        
    case XA_MIXER_CONFIG_PARAM_SAMPLE_RATE:
        /* ...return mixer sample rate */
        *(WORD32 *)pv_value = d->sample_rate;
        return XA_NO_ERROR;
        
    case XA_MIXER_CONFIG_PARAM_PCM_WIDTH:
        /* ...return current PCM width */
        *(WORD32 *)pv_value = d->pcm_width;
        return XA_NO_ERROR;

    case XA_MIXER_CONFIG_PARAM_CHANNELS:
        /* ...return current channel number */
        *(WORD32 *)pv_value = d->channels;
        return XA_NO_ERROR;

    case XA_MIXER_CONFIG_PARAM_FRAME_SIZE:
        /* ...return current in/out frame length (in samples) */
        *(WORD32 *)pv_value = d->frame_size;
        return XA_NO_ERROR;

    case XA_MIXER_CONFIG_PARAM_BUFFER_SIZE:
        /* ...return current in/out frame length (in bytes) */
        *(WORD32 *)pv_value = d->buffer_size;
        return XA_NO_ERROR;

    default:
        TRACE(ERROR, _x("Invalid parameter: %X"), i_idx);
        return XA_API_FATAL_INVALID_CMD_TYPE;
    }
}

/* ...execution command */
static XA_ERRORCODE xa_mixer_execute(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...sanity check - mixer must be valid */
    XF_CHK_ERR(d, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...mixer must be in running state */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_RUNNING, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...process individual command type */
    switch (i_idx)
    {
    case XA_CMD_TYPE_DO_EXECUTE:
        /* ...perform mixing of the channels */
        return xa_mixer_do_execute_stereo_16bit(d);
        
    case XA_CMD_TYPE_DONE_QUERY:
        /* ...check if processing is complete */
        XF_CHK_ERR(pv_value, XA_API_FATAL_INVALID_CMD_TYPE);
        *(WORD32 *)pv_value = (d->state & XA_MIXER_FLAG_COMPLETE ? 1 : 0);
        return XA_NO_ERROR;
        
    case XA_CMD_TYPE_DO_RUNTIME_INIT:
        /* ...reset mixer operation */
        return xa_mixer_do_runtime_init(d);
        
    default:
        /* ...unrecognized command */
        TRACE(ERROR, _x("Invalid index: %X"), i_idx);
        return XA_API_FATAL_INVALID_CMD_TYPE;
    }
}

/* ...set number of input bytes */
static XA_ERRORCODE xa_mixer_set_input_bytes(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    u32     size;
    
    /* ...sanity check - check parameters */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...track index must be valid */
    XF_CHK_ERR(i_idx >= 0 && i_idx < XA_MIXER_MAX_TRACK_NUMBER, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...mixer must be initialized */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_POSTINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...input buffer must exist */
    XF_CHK_ERR(d->input[i_idx], XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...input frame length should not be zero (in bytes) */
    XF_CHK_ERR((size = (u32)*(WORD32 *)pv_value) > 0, XA_MIXER_EXEC_NONFATAL_INPUT);

    /* ...all is correct; set input buffer length in bytes */
    d->input_length[i_idx] = size;
    
    return XA_NO_ERROR;
}

/* ...get number of output bytes */
static XA_ERRORCODE xa_mixer_get_output_bytes(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...sanity check - check parameters */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...track index must be zero */
    XF_CHK_ERR(i_idx == XA_MIXER_MAX_TRACK_NUMBER, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...mixer must be running */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_RUNNING, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...output buffer must exist */
    XF_CHK_ERR(d->output, XA_MIXER_EXEC_NONFATAL_OUTPUT);

    /* ...return number of produced bytes */
    *(WORD32 *)pv_value = (d->state & XA_MIXER_FLAG_OUTPUT ? d->buffer_size : 0);

    return XA_NO_ERROR;
}

/* ...get number of consumed bytes */
static XA_ERRORCODE xa_mixer_get_curidx_input_buf(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...sanity check - check parameters */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...track index must be valid */
    XF_CHK_ERR(i_idx >= 0 && i_idx < XA_MIXER_MAX_TRACK_NUMBER, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...mixer must be running */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_RUNNING, XA_MIXER_EXEC_FATAL_STATE);
    
    /* ...input buffer must exist */
    XF_CHK_ERR(d->input[i_idx], XA_MIXER_EXEC_FATAL_INPUT);

    /* ...return number of bytes consumed (always consume fixed-length chunk) */
    *(WORD32 *)pv_value = d->input_length[i_idx], d->input_length[i_idx] = 0;
    
    return XA_NO_ERROR;
}

/*******************************************************************************
 * Memory information API
 ******************************************************************************/

/* ..get total amount of data for memory tables */
static XA_ERRORCODE xa_mixer_get_memtabs_size(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...basic sanity checks */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...check mixer is pre-initialized */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_PREINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...we have all our tables inside API structure - good? tbd */
    *(WORD32 *)pv_value = 0;
    
    return XA_NO_ERROR;
}

/* ..set memory tables pointer */
static XA_ERRORCODE xa_mixer_set_memtabs_ptr(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...basic sanity checks */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...check mixer is pre-initialized */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_PREINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...do not do anything; just return success - tbd */
    return XA_NO_ERROR;
}

/* ...return total amount of memory buffers */
static XA_ERRORCODE xa_mixer_get_n_memtabs(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...basic sanity checks */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...we have N input buffers, 1 output buffer and 1 scratch buffer */
    *(WORD32 *)pv_value = XA_MIXER_MAX_TRACK_NUMBER + 1 + 1;
    
    return XA_NO_ERROR;
}

/* ...return memory buffer data */
static XA_ERRORCODE xa_mixer_get_mem_info_size(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...basic sanity check */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...return frame buffer minimal size only after post-initialization is done */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_POSTINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...all buffers are of the same length */
    *(WORD32 *)pv_value = (WORD32) d->buffer_size;
        
    return XA_NO_ERROR;
}

/* ...return memory alignment data */
static XA_ERRORCODE xa_mixer_get_mem_info_alignment(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...basic sanity check */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...return frame buffer minimal size only after post-initialization is done */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_POSTINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...all buffers are 4-bytes aligned */
    *(WORD32 *)pv_value = 4;
        
    return XA_NO_ERROR;
}

/* ...return memory type data */
static XA_ERRORCODE xa_mixer_get_mem_info_type(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...basic sanity check */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...return frame buffer minimal size only after post-initialization is done */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_POSTINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);

    switch (i_idx)
    {
    case 0 ... XA_MIXER_MAX_TRACK_NUMBER - 1:
        /* ...input buffers */
        *(WORD32 *)pv_value = XA_MEMTYPE_INPUT;
        return XA_NO_ERROR;
        
    case XA_MIXER_MAX_TRACK_NUMBER:
        /* ...output buffer */
        *(WORD32 *)pv_value = XA_MEMTYPE_OUTPUT;
        return XA_NO_ERROR;
        
    case XA_MIXER_MAX_TRACK_NUMBER + 1:
        /* ...scratch buffer */
        *(WORD32 *)pv_value = XA_MEMTYPE_SCRATCH;
        return XA_NO_ERROR;
        
    default:
        /* ...invalid index */
        return XF_CHK_ERR(0, XA_API_FATAL_INVALID_CMD_TYPE);
    }
}

/* ...set memory pointer */
static XA_ERRORCODE xa_mixer_set_mem_ptr(XAPcmMixer *d, WORD32 i_idx, pVOID pv_value)
{
    /* ...basic sanity check */
    XF_CHK_ERR(d && pv_value, XA_API_FATAL_INVALID_CMD_TYPE);

    /* ...codec must be initialized */
    XF_CHK_ERR(d->state & XA_MIXER_FLAG_POSTINIT_DONE, XA_API_FATAL_INVALID_CMD_TYPE);
    
    /* ...select memory buffer */
    switch (i_idx)
    {
    case 0 ... XA_MIXER_MAX_TRACK_NUMBER - 1:
        /* ...input buffers */
        d->input[i_idx] = pv_value;
        return XA_NO_ERROR;
        
    case XA_MIXER_MAX_TRACK_NUMBER:
        /* ...output buffer */
        d->output = pv_value;
        return XA_NO_ERROR;
        
    case XA_MIXER_MAX_TRACK_NUMBER + 1:
        /* ...scratch buffer */
        d->scratch = pv_value;
        return XA_NO_ERROR;
        
    default:
        /* ...invalid index */
        return XF_CHK_ERR(0, XA_API_FATAL_INVALID_CMD_TYPE);
    }
}

/*******************************************************************************
 * API command hooks
 ******************************************************************************/

static XA_ERRORCODE (* const xa_mixer_api[])(XAPcmMixer *, WORD32, pVOID) = 
{
    [XA_API_CMD_GET_API_SIZE]           = xa_mixer_get_api_size,

    [XA_API_CMD_INIT]                   = xa_mixer_init, 
    [XA_API_CMD_SET_CONFIG_PARAM]       = xa_mixer_set_config_param,
    [XA_API_CMD_GET_CONFIG_PARAM]       = xa_mixer_get_config_param,

    [XA_API_CMD_EXECUTE]                = xa_mixer_execute,
    [XA_API_CMD_SET_INPUT_BYTES]        = xa_mixer_set_input_bytes,
    [XA_API_CMD_GET_OUTPUT_BYTES]       = xa_mixer_get_output_bytes,
    [XA_API_CMD_GET_CURIDX_INPUT_BUF]   = xa_mixer_get_curidx_input_buf,

    [XA_API_CMD_GET_MEMTABS_SIZE]       = xa_mixer_get_memtabs_size,
    [XA_API_CMD_SET_MEMTABS_PTR]        = xa_mixer_set_memtabs_ptr,
    [XA_API_CMD_GET_N_MEMTABS]          = xa_mixer_get_n_memtabs,
    [XA_API_CMD_GET_MEM_INFO_SIZE]      = xa_mixer_get_mem_info_size,
    [XA_API_CMD_GET_MEM_INFO_ALIGNMENT] = xa_mixer_get_mem_info_alignment,
    [XA_API_CMD_GET_MEM_INFO_TYPE]      = xa_mixer_get_mem_info_type,
    [XA_API_CMD_SET_MEM_PTR]            = xa_mixer_set_mem_ptr,
};

/* ...total numer of commands supported */
#define XA_MIXER_API_COMMANDS_NUM   (sizeof(xa_mixer_api) / sizeof(xa_mixer_api[0]))

/*******************************************************************************
 * API entry point
 ******************************************************************************/

XA_ERRORCODE xa_mixer(xa_codec_handle_t p_xa_module_obj, WORD32 i_cmd, WORD32 i_idx, pVOID pv_value)
{
    XAPcmMixer    *d = (XAPcmMixer *) p_xa_module_obj;

    /* ...check if command index is sane */
    XF_CHK_ERR(i_cmd < XA_MIXER_API_COMMANDS_NUM, XA_API_FATAL_INVALID_CMD);
    
    /* ...see if command is defined */
    XF_CHK_ERR(xa_mixer_api[i_cmd], XA_API_FATAL_INVALID_CMD);
    
    /* ...execute requested command */
    return xa_mixer_api[i_cmd](d, i_idx, pv_value);
}