forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.c
More file actions
1110 lines (928 loc) · 30.8 KB
/
selector.c
File metadata and controls
1110 lines (928 loc) · 30.8 KB
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2019 Intel Corporation. All rights reserved.
//
// Author: Lech Betlej <lech.betlej@linux.intel.com>
/**
* \file
* \brief Audio channel selection component. In case 1 output channel is
* \brief selected in topology the component provides the selected channel on
* \brief output. In case 2 or 4 channels are selected on output the component
* \brief works in a passthrough mode.
* \authors Lech Betlej <lech.betlej@linux.intel.com>
*/
#include <sof/audio/component.h>
#include <sof/audio/pipeline.h>
#include <sof/audio/selector.h>
#include <sof/audio/ipc-config.h>
#include <sof/common.h>
#include <rtos/panic.h>
#include <sof/ipc/msg.h>
#include <rtos/alloc.h>
#include <rtos/init.h>
#include <sof/lib/memory.h> /* for SHARED_DATA */
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <rtos/string.h>
#include <sof/trace/trace.h>
#include <sof/ut.h>
#include <ipc/control.h>
#include <ipc/stream.h>
#include <ipc/topology.h>
#include <kernel/abi.h>
#include <user/trace.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#if CONFIG_IPC_MAJOR_4
#define SEL_MAX_CONFIG_BLOB_SIZE (SEL_MAX_NUM_CONFIGS * sizeof(struct ipc4_selector_coeffs_config))
#endif
LOG_MODULE_REGISTER(selector, CONFIG_SOF_LOG_LEVEL);
#if CONFIG_IPC_MAJOR_3
static const struct comp_driver comp_selector;
SOF_DEFINE_REG_UUID(selector);
static int selector_verify_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct comp_buffer *buffer, *sinkb;
uint32_t in_channels;
uint32_t out_channels;
comp_dbg(dev, "entry");
sinkb = comp_dev_get_first_data_consumer(dev);
/* check whether params->channels (received from driver) are equal to
* cd->config.in_channels_count (PLAYBACK) or
* cd->config.out_channels_count (CAPTURE) set during creating selector
* component in selector_new() or in selector_ctrl_set_data().
* cd->config.in/out_channels_count = 0 means that it can vary.
*/
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
/* fetch sink buffer for playback */
buffer = comp_dev_get_first_data_consumer(dev);
if (cd->config.in_channels_count &&
cd->config.in_channels_count != params->channels) {
comp_err(dev, "src in_channels_count does not match pcm channels");
return -EINVAL;
}
in_channels = cd->config.in_channels_count;
/* if cd->config.out_channels_count are equal to 0
* (it can vary), we set params->channels to sink buffer
* channels, which were previously set in
* pipeline_comp_hw_params()
*/
out_channels = cd->config.out_channels_count ?
cd->config.out_channels_count : audio_stream_get_channels(&buffer->stream);
params->channels = out_channels;
} else {
/* fetch source buffer for capture */
buffer = comp_dev_get_first_data_producer(dev);
if (cd->config.out_channels_count &&
cd->config.out_channels_count != params->channels) {
comp_err(dev, "src in_channels_count does not match pcm channels");
return -EINVAL;
}
out_channels = cd->config.out_channels_count;
/* if cd->config.in_channels_count are equal to 0
* (it can vary), we set params->channels to source buffer
* channels, which were previously set in
* pipeline_comp_hw_params()
*/
in_channels = cd->config.in_channels_count ?
cd->config.in_channels_count : audio_stream_get_channels(&buffer->stream);
params->channels = in_channels;
}
/* Set buffer params */
buffer_set_params(buffer, params, BUFFER_UPDATE_FORCE);
/* set component period frames */
component_set_nearest_period_frames(dev, audio_stream_get_rate(&sinkb->stream));
/* verify input channels */
switch (in_channels) {
case SEL_SOURCE_2CH:
case SEL_SOURCE_4CH:
break;
default:
comp_err(dev, "in_channels = %u", in_channels);
return -EINVAL;
}
/* verify output channels */
switch (out_channels) {
case SEL_SINK_1CH:
break;
case SEL_SINK_2CH:
case SEL_SINK_4CH:
/* verify proper channels for passthrough mode */
if (in_channels != out_channels) {
comp_err(dev, "in_channels = %u, out_channels = %u"
, in_channels, out_channels);
return -EINVAL;
}
break;
default:
comp_err(dev, "out_channels = %u"
, out_channels);
return -EINVAL;
}
if (cd->config.sel_channel > (params->channels - 1)) {
comp_err(dev, "ch_idx = %u"
, cd->config.sel_channel);
return -EINVAL;
}
return 0;
}
static struct comp_dev *selector_new(const struct comp_driver *drv,
const struct comp_ipc_config *config,
const void *spec)
{
const struct ipc_config_process *ipc_process = spec;
size_t bs = ipc_process->size;
struct comp_dev *dev;
struct comp_data *cd;
int ret;
comp_cl_dbg(&comp_selector, "selector_new()");
dev = comp_alloc(drv, sizeof(*dev));
if (!dev)
return NULL;
dev->ipc_config = *config;
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
if (!cd) {
comp_free_device(dev);
return NULL;
}
comp_set_drvdata(dev, cd);
ret = memcpy_s(&cd->config, sizeof(cd->config), ipc_process->data, bs);
if (ret) {
rfree(cd);
comp_free_device(dev);
return NULL;
}
dev->state = COMP_STATE_READY;
return dev;
}
/**
* \brief Frees selector component.
* \param[in,out] dev Selector base component device.
*/
static void selector_free(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
comp_dbg(dev, "entry");
rfree(cd);
comp_free_device(dev);
}
/**
* \brief Sets selector component audio stream parameters.
* \param[in,out] dev Selector base component device.
* \return Error code.
*
* All done in prepare since we need to know source and sink component params.
*/
static int selector_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
int err;
comp_dbg(dev, "entry");
err = selector_verify_params(dev, params);
if (err < 0) {
comp_err(dev, "pcm params verification failed.");
return -EINVAL;
}
return 0;
}
/**
* \brief Sets selector control command.
* \param[in,out] dev Selector base component device.
* \param[in,out] cdata Control command data.
* \return Error code.
*/
static int selector_ctrl_set_data(struct comp_dev *dev,
struct sof_ipc_ctrl_data *cdata)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct sof_sel_config *cfg;
int ret = 0;
switch (cdata->cmd) {
case SOF_CTRL_CMD_BINARY:
comp_dbg(dev, "SOF_CTRL_CMD_BINARY");
cfg = (struct sof_sel_config *)
ASSUME_ALIGNED(&cdata->data->data, 4);
/* Just set the configuration */
cd->config.in_channels_count = cfg->in_channels_count;
cd->config.out_channels_count = cfg->out_channels_count;
cd->config.sel_channel = cfg->sel_channel;
break;
default:
comp_err(dev, "invalid cdata->cmd = %u",
cdata->cmd);
ret = -EINVAL;
break;
}
return ret;
}
/**
* \brief Gets selector control command.
* \param[in,out] dev Selector base component device.
* \param[in,out] cdata Selector command data.
* \param[in] size Command data size.
* \return Error code.
*/
static int selector_ctrl_get_data(struct comp_dev *dev,
struct sof_ipc_ctrl_data *cdata, int size)
{
struct comp_data *cd = comp_get_drvdata(dev);
int ret = 0;
switch (cdata->cmd) {
case SOF_CTRL_CMD_BINARY:
comp_dbg(dev, "SOF_CTRL_CMD_BINARY");
if (size < sizeof(cd->config))
return -EINVAL;
/* Copy back to user space */
ret = memcpy_s(cdata->data->data,
size, &cd->config,
sizeof(cd->config));
assert(!ret);
cdata->data->abi = SOF_ABI_VERSION;
cdata->data->size = sizeof(cd->config);
break;
default:
comp_err(dev, "invalid cdata->cmd");
ret = -EINVAL;
break;
}
return ret;
}
/**
* \brief Used to pass standard and bespoke commands (with data) to component.
* \param[in,out] dev Selector base component device.
* \param[in] cmd Command type.
* \param[in,out] data Control command data.
* \param[in] max_data_size Command max data size.
* \return Error code.
*/
static int selector_cmd(struct comp_dev *dev, int cmd, void *data,
int max_data_size)
{
struct sof_ipc_ctrl_data *cdata = ASSUME_ALIGNED(data, 4);
int ret = 0;
comp_dbg(dev, "entry");
switch (cmd) {
case COMP_CMD_SET_DATA:
ret = selector_ctrl_set_data(dev, cdata);
break;
case COMP_CMD_GET_DATA:
ret = selector_ctrl_get_data(dev, cdata, max_data_size);
break;
case COMP_CMD_SET_VALUE:
comp_dbg(dev, "COMP_CMD_SET_VALUE");
break;
case COMP_CMD_GET_VALUE:
comp_dbg(dev, "COMP_CMD_GET_VALUE");
break;
default:
comp_err(dev, "invalid command");
ret = -EINVAL;
}
return ret;
}
/**
* \brief Sets component state.
* \param[in,out] dev Selector base component device.
* \param[in] cmd Command type.
* \return Error code.
*/
static int selector_trigger(struct comp_dev *dev, int cmd)
{
struct comp_buffer *sourceb;
enum sof_comp_type type;
int ret;
comp_dbg(dev, "entry");
sourceb = comp_dev_get_first_data_producer(dev);
if (!sourceb) {
comp_err(dev, "source disconnected");
return -ENODEV;
}
ret = comp_set_state(dev, cmd);
if (ret == COMP_STATUS_STATE_ALREADY_SET)
ret = 0;
/* TODO: remove in the future after adding support for case when
* kpb_init_draining() and kpb_draining_task() are interrupted by
* new pipeline_task()
*/
type = dev_comp_type(comp_buffer_get_source_component(sourceb));
return type == SOF_COMP_KPB ? PPL_STATUS_PATH_TERMINATE : ret;
}
/**
* \brief Copies and processes stream data.
* \param[in,out] dev Selector base component device.
* \return Error code.
*/
static int selector_copy(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct comp_buffer *sink, *source;
uint32_t frames;
uint32_t source_bytes;
uint32_t sink_bytes;
comp_dbg(dev, "entry");
/* selector component will have 1 source and 1 sink buffer */
source = comp_dev_get_first_data_producer(dev);
sink = comp_dev_get_first_data_consumer(dev);
if (!audio_stream_get_avail(&source->stream))
return PPL_STATUS_PATH_STOP;
frames = audio_stream_avail_frames(&source->stream, &sink->stream);
source_bytes = frames * audio_stream_frame_bytes(&source->stream);
sink_bytes = frames * audio_stream_frame_bytes(&sink->stream);
comp_dbg(dev, "source_bytes = 0x%x, sink_bytes = 0x%x",
source_bytes, sink_bytes);
/* copy selected channels from in to out */
buffer_stream_invalidate(source, source_bytes);
cd->sel_func(dev, &sink->stream, &source->stream, frames);
buffer_stream_writeback(sink, sink_bytes);
/* calculate new free and available */
comp_update_buffer_produce(sink, sink_bytes);
comp_update_buffer_consume(source, source_bytes);
return 0;
}
/**
* \brief Prepares selector component for processing.
* \param[in,out] dev Selector base component device.
* \return Error code.
*/
static int selector_prepare(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct comp_buffer *sinkb, *sourceb;
size_t sink_size;
int ret;
comp_dbg(dev, "entry");
ret = comp_set_state(dev, COMP_TRIGGER_PREPARE);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
/* selector component will have 1 source and 1 sink buffer */
sourceb = comp_dev_get_first_data_producer(dev);
sinkb = comp_dev_get_first_data_consumer(dev);
if (!sourceb || !sinkb) {
comp_err(dev, "no source or sink buffer");
return -ENOTCONN;
}
/* get source data format and period bytes */
cd->source_format = audio_stream_get_frm_fmt(&sourceb->stream);
cd->source_period_bytes = audio_stream_period_bytes(&sourceb->stream, dev->frames);
/* get sink data format and period bytes */
cd->sink_format = audio_stream_get_frm_fmt(&sinkb->stream);
cd->sink_period_bytes = audio_stream_period_bytes(&sinkb->stream, dev->frames);
/* There is an assumption that sink component will report out
* proper number of channels [1] for selector to actually
* reduce channel count between source and sink
*/
comp_dbg(dev, "sourceb->schannels = %u",
audio_stream_get_channels(&sourceb->stream));
comp_dbg(dev, "sinkb->channels = %u",
audio_stream_get_channels(&sinkb->stream));
sink_size = audio_stream_get_size(&sinkb->stream);
if (sink_size < cd->sink_period_bytes) {
comp_err(dev, "sink buffer size %zu is insufficient < %d",
sink_size, cd->sink_period_bytes);
ret = -ENOMEM;
goto err;
}
/* validate */
if (cd->sink_period_bytes == 0) {
comp_err(dev, "cd->sink_period_bytes = 0, dev->frames = %u",
dev->frames);
ret = -EINVAL;
goto err;
}
if (cd->source_period_bytes == 0) {
comp_err(dev, "cd->source_period_bytes = 0, dev->frames = %u",
dev->frames);
ret = -EINVAL;
goto err;
}
cd->sel_func = sel_get_processing_function(dev);
if (!cd->sel_func) {
comp_err(dev, "invalid cd->sel_func, cd->source_format = %u, cd->sink_format = %u, cd->out_channels_count = %u",
cd->source_format, cd->sink_format,
cd->config.out_channels_count);
ret = -EINVAL;
goto err;
}
return 0;
err:
comp_set_state(dev, COMP_TRIGGER_RESET);
return ret;
}
/**
* \brief Resets selector component.
* \param[in,out] dev Selector base component device.
* \return Error code.
*/
static int selector_reset(struct comp_dev *dev)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
comp_dbg(dev, "entry");
cd->source_period_bytes = 0;
cd->sink_period_bytes = 0;
cd->sel_func = NULL;
ret = comp_set_state(dev, COMP_TRIGGER_RESET);
return ret;
}
DECLARE_TR_CTX(selector_tr, SOF_UUID(selector_uuid), LOG_LEVEL_INFO);
/** \brief Selector component definition. */
static const struct comp_driver comp_selector = {
.type = SOF_COMP_SELECTOR,
.uid = SOF_RT_UUID(selector_uuid),
.tctx = &selector_tr,
.ops = {
.create = selector_new,
.free = selector_free,
.params = selector_params,
.cmd = selector_cmd,
.trigger = selector_trigger,
.copy = selector_copy,
.prepare = selector_prepare,
.reset = selector_reset,
},
};
static SHARED_DATA struct comp_driver_info comp_selector_info = {
.drv = &comp_selector,
};
/** \brief Initializes selector component. */
UT_STATIC void sys_comp_selector_init(void)
{
comp_register(platform_shared_get(&comp_selector_info,
sizeof(comp_selector_info)));
}
DECLARE_MODULE(sys_comp_selector_init);
SOF_MODULE_INIT(selector, sys_comp_selector_init);
#else
SOF_DEFINE_REG_UUID(selector4);
static void build_config(struct comp_data *cd, struct module_config *cfg)
{
enum sof_ipc_frame frame_fmt, valid_fmt;
const struct sof_selector_ipc4_config *sel_cfg = &cd->sel_ipc4_cfg;
const struct ipc4_audio_format *out_fmt;
int i;
if (cd->sel_ipc4_cfg.init_payload_fmt == IPC4_SEL_INIT_PAYLOAD_BASE_WITH_EXT)
out_fmt = &sel_cfg->pin_cfg.out_pin.audio_fmt;
else
out_fmt = &sel_cfg->output_format;
audio_stream_fmt_conversion(cfg->base_cfg.audio_fmt.depth,
cfg->base_cfg.audio_fmt.valid_bit_depth,
&frame_fmt, &valid_fmt,
cfg->base_cfg.audio_fmt.s_type);
cd->source_format = frame_fmt;
audio_stream_fmt_conversion(out_fmt->depth, out_fmt->valid_bit_depth,
&frame_fmt, &valid_fmt, out_fmt->s_type);
cd->sink_format = frame_fmt;
cd->config.in_channels_count = cfg->base_cfg.audio_fmt.channels_count;
cd->config.out_channels_count = out_fmt->channels_count;
/* Build default coefficient array (unity Q10 on diagonal, i.e. pass-through mode) */
memset(&cd->coeffs_config, 0, sizeof(cd->coeffs_config));
for (i = 0; i < MIN(SEL_SOURCE_CHANNELS_MAX, SEL_SINK_CHANNELS_MAX); i++)
cd->coeffs_config.coeffs[i][i] = SEL_COEF_ONE_Q10;
}
static int selector_init(struct processing_module *mod)
{
struct module_data *md = &mod->priv;
struct module_config *cfg = &md->cfg;
const struct ipc4_base_module_extended_cfg *init_cfg_ext;
const struct sof_selector_avs_ipc4_config *init_cfg_out_fmt;
enum ipc4_selector_init_payload_fmt payload_fmt;
struct comp_data *cd;
size_t base_cfg_size;
size_t bs[2];
int ret;
comp_dbg(mod->dev, "entry");
init_cfg_ext = cfg->init_data;
init_cfg_out_fmt = cfg->init_data;
base_cfg_size = sizeof(struct ipc4_base_module_cfg);
bs[0] = ipc4_calc_base_module_cfg_ext_size(SEL_NUM_IN_PIN_FMTS,
SEL_NUM_OUT_PIN_FMTS);
bs[1] = sizeof(struct ipc4_audio_format);
if (cfg->size == base_cfg_size + bs[0]) {
payload_fmt = IPC4_SEL_INIT_PAYLOAD_BASE_WITH_EXT;
if (init_cfg_ext->base_cfg_ext.nb_input_pins != SEL_NUM_IN_PIN_FMTS ||
init_cfg_ext->base_cfg_ext.nb_output_pins != SEL_NUM_OUT_PIN_FMTS) {
comp_err(mod->dev, "Invalid pin configuration");
return -EINVAL;
}
} else if (cfg->size == base_cfg_size + bs[1]) {
payload_fmt = IPC4_SEL_INIT_PAYLOAD_BASE_WITH_OUT_FMT;
} else {
comp_err(mod->dev, "Invalid configuration size");
return -EINVAL;
}
cd = mod_zalloc(mod, sizeof(*cd));
if (!cd)
return -ENOMEM;
cd->sel_ipc4_cfg.init_payload_fmt = payload_fmt;
md->private = cd;
if (payload_fmt == IPC4_SEL_INIT_PAYLOAD_BASE_WITH_EXT) {
size_t size = sizeof(struct sof_selector_ipc4_pin_config);
ret = memcpy_s(&cd->sel_ipc4_cfg.pin_cfg, size,
init_cfg_ext->base_cfg_ext.pin_formats, size);
} else {
ret = memcpy_s(&cd->sel_ipc4_cfg.output_format, bs[1],
&init_cfg_out_fmt->output_format, bs[1]);
}
assert(!ret);
build_config(cd, cfg);
return 0;
}
static void set_selector_params(struct processing_module *mod,
struct sof_ipc_stream_params *params)
{
struct comp_dev *dev = mod->dev;
struct comp_data *cd = module_get_private_data(mod);
const struct sof_selector_ipc4_config *sel_cfg = &cd->sel_ipc4_cfg;
const struct ipc4_audio_format *out_fmt = NULL;
struct comp_buffer *src_buf;
int i;
if (cd->sel_ipc4_cfg.init_payload_fmt == IPC4_SEL_INIT_PAYLOAD_BASE_WITH_EXT)
out_fmt = &sel_cfg->pin_cfg.out_pin.audio_fmt;
else
out_fmt = &sel_cfg->output_format;
if (dev->direction == SOF_IPC_STREAM_PLAYBACK)
params->channels = cd->config.in_channels_count;
else
params->channels = cd->config.out_channels_count;
params->rate = mod->priv.cfg.base_cfg.audio_fmt.sampling_frequency;
params->frame_fmt = cd->source_format;
for (i = 0; i < SOF_IPC_MAX_CHANNELS; i++)
params->chmap[i] = (out_fmt->ch_map >> i * 4) & 0xf;
/* update each sink format */
struct comp_buffer *sink_buf;
comp_dev_for_each_consumer(dev, sink_buf) {
ipc4_update_buffer_format(sink_buf, out_fmt);
audio_stream_set_channels(&sink_buf->stream, params->channels);
audio_stream_set_rate(&sink_buf->stream, params->rate);
}
/* update the source format
* used only for rare cases where two pipelines are connected by a shared
* buffer and 2 copiers, this will set source format only for shared buffers
* for a short time when the second pipeline already started
* and the first one is not ready yet along with sink buffers params
*/
src_buf = comp_dev_get_first_data_producer(dev);
if (!audio_buffer_hw_params_configured(&src_buf->audio_buffer))
ipc4_update_buffer_format(src_buf, &mod->priv.cfg.base_cfg.audio_fmt);
}
static int selector_verify_params(struct processing_module *mod,
struct sof_ipc_stream_params *params)
{
struct comp_dev *dev = mod->dev;
struct comp_data *cd = module_get_private_data(mod);
struct comp_buffer *buffer;
uint32_t in_channels = cd->config.in_channels_count;
uint32_t out_channels = cd->config.out_channels_count;
comp_dbg(dev, "entry");
/* verify input channels */
if (in_channels == 0 || in_channels > SEL_SOURCE_CHANNELS_MAX) {
comp_err(dev, "in_channels = %u", in_channels);
return -EINVAL;
}
/* verify output channels */
if (out_channels == 0 || out_channels > SEL_SINK_CHANNELS_MAX) {
comp_err(dev, "out_channels = %u", out_channels);
return -EINVAL;
}
/* apply input/output channels count according to stream direction */
if (dev->direction == SOF_IPC_STREAM_PLAYBACK) {
params->channels = out_channels;
buffer = comp_dev_get_first_data_consumer(dev);
} else {
params->channels = in_channels;
buffer = comp_dev_get_first_data_producer(dev);
}
buffer_set_params(buffer, params, BUFFER_UPDATE_FORCE);
/* set component period frames */
buffer = comp_dev_get_first_data_consumer(dev);
component_set_nearest_period_frames(dev, audio_stream_get_rate(&buffer->stream));
return 0;
}
/**
* \brief Frees selector component.
* \param[in,out] mod Selector base module device.
*/
static int selector_free(struct processing_module *mod)
{
struct comp_data *cd = module_get_private_data(mod);
comp_dbg(mod->dev, "entry");
mod_free(mod, cd->multi_coeffs_config);
mod_free(mod, cd);
return 0;
}
/**
* \brief Sets selector component audio stream parameters.
* \param[in,out] mod Selector base module device.
* \return Error code.
*
* All done in prepare since we need to know source and sink component params.
*/
static int selector_params(struct processing_module *mod)
{
struct sof_ipc_stream_params *params = mod->stream_params;
int err;
comp_dbg(mod->dev, "entry");
set_selector_params(mod, params);
err = selector_verify_params(mod, params);
if (err < 0) {
comp_err(mod->dev, "pcm params verification failed.");
return -EINVAL;
}
return 0;
}
static int selector_set_config(struct processing_module *mod, uint32_t config_id,
enum module_cfg_fragment_position pos, uint32_t data_offset_size,
const uint8_t *fragment, size_t fragment_size, uint8_t *response,
size_t response_size)
{
struct comp_data *cd = module_get_private_data(mod);
struct comp_dev *dev = mod->dev;
int n;
if (config_id == IPC4_SELECTOR_COEFFS_CONFIG_ID) {
if (data_offset_size > SEL_MAX_CONFIG_BLOB_SIZE ||
fragment_size < data_offset_size ||
pos != MODULE_CFG_FRAGMENT_SINGLE) {
comp_err(dev, "Failure with data_offset_size %u fragment_size %u pos %u",
data_offset_size, (uint32_t)fragment_size, (uint32_t)pos);
return -EINVAL;
}
/* The size must be N times the coefficient vectors size of one channels
* up/down mix profile.
*/
n = data_offset_size / sizeof(struct ipc4_selector_coeffs_config);
if (n < 1 || data_offset_size != n * sizeof(struct ipc4_selector_coeffs_config)) {
comp_err(dev, "Invalid configuration size.");
return -EINVAL;
}
cd->num_configs = n;
if (cd->multi_coeffs_config && cd->multi_coeffs_config_size < data_offset_size) {
/* Configuration exist but the allocation is too small to write over. */
mod_free(mod, cd->multi_coeffs_config);
cd->multi_coeffs_config = NULL;
}
if (!cd->multi_coeffs_config) {
cd->multi_coeffs_config_size = data_offset_size;
cd->multi_coeffs_config = mod_alloc(mod, cd->multi_coeffs_config_size);
if (!cd->multi_coeffs_config) {
comp_err(dev, "Failed to allocate configuration blob.");
return -ENOMEM;
}
}
/* Copy the configuration and notify for need to re-configure. If for
* some reason the memcpy_s() would return an error (it can't because
* of the previous checks), the partial or incorrect blob would remain
* allocated but be freed later in module error handling and ending.
*/
cd->new_config = true;
return memcpy_s(cd->multi_coeffs_config, cd->multi_coeffs_config_size,
fragment, data_offset_size);
}
return -EINVAL;
}
static int selector_get_config(struct processing_module *mod, uint32_t config_id,
uint32_t *data_offset_size, uint8_t *fragment, size_t fragment_size)
{
/* ToDo: add support */
return 0;
}
/**
* \brief Loop the array of mix coefficients sets and find a set with matching channels
* in and out count.
* \param[in] cd Selector component data.
* \param[in] source_channels Number of channels in source.
* \param[in] sink_channels Number of channels in sink.
*
* \return Pointer to the matching ipc4_selector_coeffs_config if found, or NULL if
* no matching configuration exists.
*/
static struct ipc4_selector_coeffs_config *selector_config_array_search(struct comp_data *cd,
int source_channels,
int sink_channels)
{
struct ipc4_selector_coeffs_config *found = NULL;
int i;
for (i = 0; i < cd->num_configs; i++) {
if (cd->multi_coeffs_config[i].source_channels_count == source_channels &&
cd->multi_coeffs_config[i].sink_channels_count == sink_channels) {
found = &cd->multi_coeffs_config[i];
break;
}
}
return found;
}
/**
* \brief Get mix coefficients set from configuration blob with multiple coefficients sets.
* Also activate more efficient pass-through copy mode if the coefficients indicate 1:1
* copy from source to sink.
* \param[in,out] mod Selector base module device.
* \param[in] source_channels Number of channels in source.
* \param[in] sink_channels Number of channels in sink.
*
* \return Error code.
*/
static int selector_find_coefficients(struct processing_module *mod)
{
struct comp_data *cd = module_get_private_data(mod);
struct comp_dev *dev = mod->dev;
struct ipc4_selector_coeffs_config *config;
uint32_t source_channels = cd->config.in_channels_count;
uint32_t sink_channels = cd->config.out_channels_count;
int16_t coef;
int ret, i, j;
/* In set_config() the blob is copied to cd->multi_coeffs_config. A legacy blob contains a
* single set of mix coefficients without channels information. A new blob with multiple
* configurations has the source and sink channels count information. If there has been no
* set_config(), then the cd->coeffs_config has been initialized in set_selector_params()
* to mix with coefficients SEL_COEF_ONE_Q10 for matching input and output channels.
*/
if (cd->multi_coeffs_config) {
if (cd->num_configs > 1) {
config = selector_config_array_search(cd, source_channels, sink_channels);
/* If not found, check if pass-through mix is defined for the max
* channels count (8).
*/
if (!config && source_channels == sink_channels)
config = selector_config_array_search(cd, SEL_SOURCE_CHANNELS_MAX,
SEL_SINK_CHANNELS_MAX);
if (!config) {
comp_err(dev, "No mix coefficients found for %u to %u channels.",
source_channels, sink_channels);
return -EINVAL;
}
} else {
config = cd->multi_coeffs_config;
}
ret = memcpy_s(&cd->coeffs_config, sizeof(struct ipc4_selector_coeffs_config),
config, sizeof(*config));
if (ret)
return ret;
}
/* The pass-through copy function can be used if coefficients are a unit matrix for
* 1:1 stream copy.
*/
if (source_channels == sink_channels) {
cd->passthrough = true;
for (i = 0; i < sink_channels; i++) {
for (j = 0; j < source_channels; j++) {
coef = cd->coeffs_config.coeffs[i][j];
if ((i == j && coef != SEL_COEF_ONE_Q10) || (i != j && coef != 0)) {
cd->passthrough = false;
break;
}
}
}
} else {
cd->passthrough = false;
}
if (cd->passthrough)
comp_info(dev, "Passthrough mode.");
else
comp_info(dev, "Using coefficients for %u to %u channels.",
source_channels, sink_channels);
return 0;
}
/**
* \brief Copies and processes stream data.
* \param[in,out] mod Selector base module device.
* \return Error code.
*/
static int selector_process(struct processing_module *mod,
struct input_stream_buffer *input_buffers,
int num_input_buffers,
struct output_stream_buffer *output_buffers,
int num_output_buffers)
{
struct audio_stream *source;
struct audio_stream *sink;
struct comp_data *cd = module_get_private_data(mod);
uint32_t avail_frames = input_buffers[0].size;
int ret;
comp_dbg(mod->dev, "entry");
if (cd->new_config) {
cd->new_config = false;
ret = selector_find_coefficients(mod);
if (ret)
return ret;
}
if (cd->passthrough) {
source = input_buffers->data;
sink = output_buffers->data;
audio_stream_copy(source, 0, sink, 0, avail_frames * cd->config.in_channels_count);
module_update_buffer_position(input_buffers, output_buffers, avail_frames);
return 0;
}
if (avail_frames)
/* copy selected channels from in to out */
cd->sel_func(mod, input_buffers, output_buffers, avail_frames);
return 0;
}
/**
* \brief Prepares selector component for processing.
* \param[in,out] mod Selector base module device.
* \return Error code.
*/
static int selector_prepare(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
struct comp_data *cd = module_get_private_data(mod);
struct module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
struct comp_buffer *sinkb, *sourceb;
size_t sink_size;
int ret;
comp_dbg(dev, "entry");
ret = selector_params(mod);
if (ret < 0)