summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/sound/component/aw-alsa-lib/pcm_extplug.c
blob: cc528f33361100de368bf956f29f676ae57b44df (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
/*
* Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
*
* Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
* the the people's Republic of China and other countries.
* All Allwinner Technology Co.,Ltd. trademarks are used with permission.
*
* DISCLAIMER
* THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
* IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
* IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
* ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
* ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
* COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
* YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
*
*
* THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
* PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
* THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
* OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <aw-alsa-lib/pcm_extplug.h>
#include "pcm_local.h"
#include "pcm_plugin_generic.h"
#include "pcm_ext_parm.h"

typedef struct snd_pcm_extplug_priv {
    snd_pcm_plugin_t plug;
    snd_pcm_extplug_t *data;
    struct snd_ext_parm params[SND_PCM_EXTPLUG_HW_PARAMS];
    struct snd_ext_parm sparams[SND_PCM_EXTPLUG_HW_PARAMS];
} extplug_priv_t;


static const int hw_params_type[SND_PCM_EXTPLUG_HW_PARAMS] = {
    [SND_PCM_EXTPLUG_HW_FORMAT] = SND_PCM_HW_PARAM_FORMAT,
    [SND_PCM_EXTPLUG_HW_CHANNELS] = SND_PCM_HW_PARAM_CHANNELS
};

#define is_mask_type(i) (hw_params_type[i] < SND_PCM_HW_PARAM_FIRST_RANGE)

static const unsigned int excl_parbits[SND_PCM_EXTPLUG_HW_PARAMS] = {
    [SND_PCM_EXTPLUG_HW_FORMAT] = (SND_PCM_HW_PARBIT_FORMAT|
                       SND_PCM_HW_PARBIT_SAMPLE_BITS),
    [SND_PCM_EXTPLUG_HW_CHANNELS] = (SND_PCM_HW_PARBIT_CHANNELS|
                     SND_PCM_HW_PARBIT_FRAME_BITS),
};

/*
 * set min/max values for the given parameter
 */
int snd_ext_parm_set_minmax(struct snd_ext_parm *parm, unsigned int min, unsigned int max)
{
    parm->num_list = 0;
    free(parm->list);
    parm->list = NULL;
    parm->min = min;
    parm->max = max;
    parm->active = 1;
    return 0;
}

/*
 * set the list of available values for the given parameter
 */
static int val_compar(const void *ap, const void *bp)
{
    return *(const unsigned int *)ap - *(const unsigned int *)bp;
}

int snd_ext_parm_set_list(struct snd_ext_parm *parm, unsigned int num_list, const unsigned int *list)
{
    unsigned int *new_list;

    new_list = malloc(sizeof(*new_list) * num_list);
    if (new_list == NULL)
        return -ENOMEM;
    memcpy(new_list, list, sizeof(*new_list) * num_list);
    qsort(new_list, num_list, sizeof(*new_list), val_compar);

    free(parm->list);
    parm->num_list = num_list;
    parm->list = new_list;
    parm->active = 1;
    return 0;
}

void snd_ext_parm_clear(struct snd_ext_parm *parm)
{
    free(parm->list);
    memset(parm, 0, sizeof(*parm));
}

/*
 * limit the interval range to the given list
 */
static int _snd_range_list(snd_interval_t *ival, int num_list, unsigned int *list)
{
    int imin, imax;
    int changed = 0;

    if (snd_range_empty(ival))
        return -ENOENT;
    for (imin = 0; imin < num_list; imin++) {
        if (ival->range.min == list[imin] && ! ival->range.openmin)
            break;
        if (ival->range.min <= list[imin]) {
            ival->range.min = list[imin];
            ival->range.openmin = 0;
            changed = 1;
            break;
        }
    }
    if (imin >= num_list)
        return -EINVAL;
    for (imax = num_list - 1; imax >= imin; imax--) {
        if (ival->range.max == list[imax] && ! ival->range.openmax)
            break;
        if (ival->range.max >= list[imax]) {
            ival->range.max = list[imax];
            ival->range.openmax = 0;
            changed = 1;
            break;
        }
    }
    if (imax < imin)
        return -EINVAL;
    return changed;
}

/*
 * refine the range parameter
 */
int snd_ext_parm_range_refine(snd_interval_t *ival, struct snd_ext_parm *parm, int type)
{
    parm += type;
    if (!parm->active)
        return 0;
    ival->range.integer |= parm->integer;
    if (parm->num_list) {
        return _snd_range_list(ival, parm->num_list, parm->list);
    } else if (parm->min || parm->max) {
        snd_interval_t t;
        memset(&t, 0, sizeof(t));
        snd_range_set_minmax(&t, parm->min, parm->max);
        t.range.integer = ival->range.integer;
        return snd_range_refine(ival, &t);
    }
    return 0;
}

/*
 * refine the mask parameter
 */
int snd_ext_parm_mask_refine(snd_interval_t *mask, struct snd_ext_parm *parm, int type)
{
    snd_interval_t bits;
    unsigned int i;

    parm += type;
    if (!parm->active)
        return 0;
    memset(&bits, 0, sizeof(bits));
    for (i = 0; i < parm->num_list; i++)
        bits.mask |= 1U << (parm->list[i] % 32);
    return snd_mask_refine(mask, &bits);
}


int snd_pcm_extplug_set_param_list(snd_pcm_extplug_t *extplug, int type,
        unsigned int num_list, const unsigned int *list)
{
    extplug_priv_t *ext = extplug->pcm->private_data;
    if (type < 0 || type >= SND_PCM_EXTPLUG_HW_PARAMS) {
        awalsa_err("EXTPLUG: invalid parameter type %d\n", type);
        return -EINVAL;
    }
    return snd_ext_parm_set_list(&ext->params[type], num_list, list);
}

int snd_pcm_extplug_set_param_minmax(snd_pcm_extplug_t *extplug, int type,
        unsigned int min, unsigned int max)
{
    extplug_priv_t *ext = extplug->pcm->private_data;
    if (type < 0 || type >= SND_PCM_EXTPLUG_HW_PARAMS) {
        awalsa_err("EXTPLUG: invalid parameter type %d\n", type);
        return -EINVAL;
    }
    if (is_mask_type(type)) {
        awalsa_err("EXTPLUG: invalid parameter type %d\n", type);
        return -EINVAL;
    }
    return snd_ext_parm_set_minmax(&ext->params[type], min, max);
}

int snd_pcm_extplug_set_slave_param_list(snd_pcm_extplug_t *extplug, int type,
        unsigned int num_list, const unsigned int *list)
{
    extplug_priv_t *ext = extplug->pcm->private_data;
    if (type < 0 || type >= SND_PCM_EXTPLUG_HW_PARAMS) {
        awalsa_err("EXTPLUG: invalid parameter type %d\n", type);
        return -EINVAL;
    }
    return snd_ext_parm_set_list(&ext->sparams[type], num_list, list);
}

int snd_pcm_extplug_set_slave_param_minmax(snd_pcm_extplug_t *extplug, int type,
        unsigned int min, unsigned int max)
{
    extplug_priv_t *ext = extplug->pcm->private_data;
    if (type < 0 || type >= SND_PCM_EXTPLUG_HW_PARAMS) {
        awalsa_err("EXTPLUG: invalid parameter type %d\n", type);
        return -EINVAL;
    }
    if (is_mask_type(type)) {
        awalsa_err("EXTPLUG: invalid parameter type %d\n", type);
        return -EINVAL;
    }
    return snd_ext_parm_set_minmax(&ext->sparams[type], min, max);
}

static void clear_ext_params(extplug_priv_t *ext)
{
    int i;
    for (i = 0; i < SND_PCM_EXTPLUG_HW_PARAMS; i++) {
        snd_ext_parm_clear(&ext->params[i]);
        snd_ext_parm_clear(&ext->sparams[i]);
    }
}

void snd_pcm_extplug_params_reset(snd_pcm_extplug_t *extplug)
{
    extplug_priv_t *ext = extplug->pcm->private_data;
    clear_ext_params(ext);
}

/*
 * call init callback
 */
static int snd_pcm_extplug_init(snd_pcm_t *pcm)
{
    awalsa_debug("\n");
    extplug_priv_t *ext = pcm->private_data;
    return ext->data->callback->init(ext->data);
}

/*
 * write_areas skeleton - call transfer callback
 */
static snd_pcm_uframes_t
snd_pcm_extplug_write_areas(snd_pcm_t *pcm,
                const snd_pcm_channel_area_t *areas,
                snd_pcm_uframes_t offset,
                snd_pcm_uframes_t size,
                const snd_pcm_channel_area_t *slave_areas,
                snd_pcm_uframes_t slave_offset,
                snd_pcm_uframes_t *slave_sizep)
{
    extplug_priv_t *ext = pcm->private_data;

    if (size > *slave_sizep)
        size = *slave_sizep;
    size = ext->data->callback->transfer(ext->data, slave_areas, slave_offset,
                         areas, offset, size);
    *slave_sizep = size;
    return size;
}

/*
 * read_areas skeleton - call transfer callback
 */
static snd_pcm_uframes_t
snd_pcm_extplug_read_areas(snd_pcm_t *pcm,
               const snd_pcm_channel_area_t *areas,
               snd_pcm_uframes_t offset,
               snd_pcm_uframes_t size,
               const snd_pcm_channel_area_t *slave_areas,
               snd_pcm_uframes_t slave_offset,
               snd_pcm_uframes_t *slave_sizep)
{
    extplug_priv_t *ext = pcm->private_data;

    if (size > *slave_sizep)
        size = *slave_sizep;
    size = ext->data->callback->transfer(ext->data, areas, offset,
                         slave_areas, slave_offset, size);
    *slave_sizep = size;
    return size;
}

static int snd_pcm_extplug_close(snd_pcm_t *pcm)
{
    extplug_priv_t *ext = pcm->private_data;

    awalsa_debug("\n");
    snd_pcm_close(ext->plug.gen.slave);
    clear_ext_params(ext);
    if (ext->data->callback->close)
        ext->data->callback->close(ext->data);
    free(ext);
    return 0;
}

/*
 * hw_refine callback
 */
static int extplug_hw_refine(snd_pcm_hw_params_t *hw_params,
                 struct snd_ext_parm *parm)
{
    int i, err, change = 0;
    for (i = 0; i < SND_PCM_EXTPLUG_HW_PARAMS; i++) {
        int type = hw_params_type[i];
        if (is_mask_type(i))
            err = snd_ext_parm_mask_refine(hw_param_interval(hw_params, type),
                               parm, i);
        else
            err = snd_ext_parm_range_refine(hw_param_interval(hw_params, type),
                            parm, i);
        if (err < 0)
            return err;
        change |= err;
    }
    return change;
}

static int snd_pcm_extplug_hw_refine_cprepare(snd_pcm_t *pcm,
                          snd_pcm_hw_params_t *params)
{
    extplug_priv_t *ext = pcm->private_data;
    int err;
    snd_interval_t access_mask = { .mask = SND_PCM_ACCBIT_SHM };
    awalsa_debug("\n");
    err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
                     &access_mask);
    if (err < 0)
        return err;
    err = extplug_hw_refine(params, ext->params);
    if (err < 0)
        return err;
#if 0
    params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
#endif
    return 0;
}

static int snd_pcm_extplug_hw_refine_sprepare(snd_pcm_t *pcm,
                          snd_pcm_hw_params_t *sparams)
{
    extplug_priv_t *ext = pcm->private_data;
    snd_interval_t saccess_mask = { .mask = SND_PCM_ACCBIT_MMAP };
    awalsa_debug("\n");
    _snd_pcm_hw_params_any(sparams);
    _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
                   &saccess_mask);
    extplug_hw_refine(sparams, ext->sparams);
    return 0;
}

static unsigned int get_links(struct snd_ext_parm *params)
{
    int i;
    unsigned int links = (SND_PCM_HW_PARBIT_FORMAT |
                  SND_PCM_HW_PARBIT_SAMPLE_BITS |
                  SND_PCM_HW_PARBIT_CHANNELS |
                  SND_PCM_HW_PARBIT_FRAME_BITS |
                  SND_PCM_HW_PARBIT_RATE |
                  SND_PCM_HW_PARBIT_PERIODS |
                  SND_PCM_HW_PARBIT_PERIOD_SIZE |
                  SND_PCM_HW_PARBIT_PERIOD_TIME |
                  SND_PCM_HW_PARBIT_BUFFER_SIZE |
                  SND_PCM_HW_PARBIT_BUFFER_TIME);

    for (i = 0; i < SND_PCM_EXTPLUG_HW_PARAMS; i++) {
        if (params[i].active)
            links &= ~excl_parbits[i];
    }
    return links;
}

static int snd_pcm_extplug_hw_refine_schange(snd_pcm_t *pcm,
                         snd_pcm_hw_params_t *params,
                         snd_pcm_hw_params_t *sparams)
{
    extplug_priv_t *ext = pcm->private_data;
    unsigned int links = get_links(ext->sparams);

    awalsa_debug("\n");
    return _snd_pcm_hw_params_refine(sparams, links, params);
}

static int snd_pcm_extplug_hw_refine_cchange(snd_pcm_t *pcm,
                         snd_pcm_hw_params_t *params,
                         snd_pcm_hw_params_t *sparams)
{
    extplug_priv_t *ext = pcm->private_data;
    unsigned int links = get_links(ext->params);

    awalsa_debug("\n");
    return _snd_pcm_hw_params_refine(params, links, sparams);
}

static int snd_pcm_extplug_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
{
#if 0
    extplug_priv_t *ext = pcm->private_data;
    snd_pcm_t *slave = ext->plug.gen.slave;
    int ret;

    awalsa_debug("\n");

    // TODO: how to refine?
    ret = snd_pcm_hw_refine(slave, params);
    if (ret < 0)
        return ret;

    return 0;
#endif

    awalsa_debug("\n");
    int err = snd_pcm_hw_refine_slave(pcm, params,
                       snd_pcm_extplug_hw_refine_cprepare,
                       snd_pcm_extplug_hw_refine_cchange,
                       snd_pcm_extplug_hw_refine_sprepare,
                       snd_pcm_extplug_hw_refine_schange,
                       snd_pcm_generic_hw_refine);
    return err;
}

static int snd_pcm_extplug_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
{
    extplug_priv_t *ext = pcm->private_data;
    snd_pcm_t *slave = ext->plug.gen.slave;
    int err;

    awalsa_debug("\n");

    err = snd_pcm_hw_params_slave(pcm, params,
                      snd_pcm_extplug_hw_refine_cchange,
                      snd_pcm_extplug_hw_refine_sprepare,
                      snd_pcm_extplug_hw_refine_schange,
                      snd_pcm_generic_hw_params);
    if (err < 0)
        return err;

    ext->data->slave_format = slave->format;
    ext->data->slave_channels = slave->channels;
    ext->data->rate = slave->rate;
    snd_pcm_hw_params_get_format(params, &ext->data->format);
    snd_pcm_hw_params_get_channels(params, &ext->data->channels);

    if (ext->data->callback->hw_params) {
        err = ext->data->callback->hw_params(ext->data, params);
        if (err < 0)
            return err;
    }
    return 0;
}

static int snd_pcm_extplug_hw_free(snd_pcm_t *pcm)
{
    extplug_priv_t *ext = pcm->private_data;

    awalsa_debug("\n");
    snd_pcm_hw_free(ext->plug.gen.slave);
    if (ext->data->callback->hw_free)
        return ext->data->callback->hw_free(ext->data);
    return 0;
}

static void snd_pcm_extplug_dump(snd_pcm_t *pcm)
{
    extplug_priv_t *ext = pcm->private_data;
    awalsa_debug("\n");

    if (ext->data->callback->dump)
        ext->data->callback->dump(ext->data);
    else {
        if (ext->data->name)
            printf("%s\n", ext->data->name);
        else
            printf("External PCM Plugin\n");
        if (pcm->setup) {
            printf("Its setup is:\n");
            snd_pcm_dump_setup(pcm);
        }
    }
    printf("Slave: ");
    snd_pcm_dump(ext->plug.gen.slave);
}

static const snd_pcm_ops_t snd_pcm_extplug_ops = {
    .close = snd_pcm_extplug_close,
    .hw_refine = snd_pcm_extplug_hw_refine,
    .hw_params = snd_pcm_extplug_hw_params,
    .hw_free = snd_pcm_extplug_hw_free,
    .sw_params = snd_pcm_generic_sw_params,
    .channel_info = snd_pcm_generic_channel_info,
    .dump = snd_pcm_extplug_dump,
    .mmap = snd_pcm_generic_mmap,
    .munmap = snd_pcm_generic_munmap,
    //.query_chmaps = snd_pcm_extplug_query_chmaps,
    //.get_chmap = snd_pcm_extplug_get_chmap,
    //.set_chmap = snd_pcm_extplug_set_chmap,
};

int snd_pcm_extplug_create(snd_pcm_extplug_t *extplug, const char *name,
               const char *spcm_name, snd_pcm_stream_t stream, int mode)
{
    int ret = 0;
    extplug_priv_t *ext;
    snd_pcm_t *spcm, *pcm;
    const snd_pcm_config_t *sconf = NULL;

    assert(extplug && extplug->callback);
    assert(extplug->callback->transfer);

    sconf = snd_pcm_config_get_config(spcm_name);
    if (!sconf) {
        awalsa_err("can't find pcm slave: %s\n", spcm_name);
        ret = -EINVAL;
        goto err_out;
    }
    ret = snd_pcm_open_config(&spcm, sconf, stream, mode);
    if (ret < 0) {
        awalsa_err("unable to open slave\n");
        goto err_out;
    }

    ext = calloc(1, sizeof(*ext));
    if (!ext) {
        awalsa_err("no memory\n");
        ret = -ENOMEM;
        goto err_close_spcm;
    }

    ext->data = extplug;
    extplug->stream = stream;

    snd_pcm_plugin_init(&ext->plug);
    ext->plug.read = snd_pcm_extplug_read_areas;
    ext->plug.write = snd_pcm_extplug_write_areas;
    ext->plug.undo_read = snd_pcm_plugin_undo_read_generic;
    ext->plug.undo_write = snd_pcm_plugin_undo_write_generic;
    ext->plug.gen.slave = spcm;
    ext->plug.gen.close_slave = 1;
    if (extplug->callback->init)
        ext->plug.init = snd_pcm_extplug_init;

    ret = snd_pcm_new(&pcm, SND_PCM_TYPE_EXTPLUG, name, stream, mode);
    if (ret < 0) {
        goto err_free_ext;
    }

    extplug->pcm = pcm;
    pcm->ops = &snd_pcm_extplug_ops;
    pcm->fast_ops = &snd_pcm_plugin_fast_ops;
    pcm->private_data = ext;
    snd_pcm_set_hw_ptr(pcm, &ext->plug.hw_ptr, -1, 0);
    snd_pcm_set_appl_ptr(pcm, &ext->plug.appl_ptr, -1, 0);

    return 0;

err_free_ext:
    free(ext);
err_close_spcm:
    snd_pcm_close(spcm);
err_out:
    return ret;
}