forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector_generic.c
More file actions
381 lines (349 loc) · 11.8 KB
/
selector_generic.c
File metadata and controls
381 lines (349 loc) · 11.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
// 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 selector / extractor - generic processing functions
* \authors Lech Betlej <lech.betlej@linux.intel.com>
*/
#include <sof/audio/buffer.h>
#include <sof/audio/component.h>
#include <sof/audio/selector.h>
#include <sof/common.h>
#include <ipc/stream.h>
#include <stddef.h>
#include <stdint.h>
LOG_MODULE_DECLARE(selector, CONFIG_SOF_LOG_LEVEL);
#define BYTES_TO_S16_SAMPLES 1
#define BYTES_TO_S32_SAMPLES 2
#if CONFIG_IPC_MAJOR_3
#if CONFIG_FORMAT_S16LE
/**
* \brief Channel selection for 16 bit, 1 channel data format.
* \param[in,out] dev Selector base component device.
* \param[in,out] sink Destination buffer.
* \param[in,out] source Source buffer.
* \param[in] frames Number of frames to process.
*/
static void sel_s16le_1ch(struct comp_dev *dev, struct audio_stream *sink,
const struct audio_stream *source, uint32_t frames)
{
struct comp_data *cd = comp_get_drvdata(dev);
int16_t *src = audio_stream_get_rptr(source);
int16_t *dest = audio_stream_get_wptr(sink);
int16_t *src_ch;
int nmax;
int i;
int n;
int processed = 0;
const int source_frame_bytes = audio_stream_frame_bytes(source);
const unsigned int nch = audio_stream_get_channels(source);
const unsigned int sel_channel = cd->config.sel_channel; /* 0 to nch - 1 */
while (processed < frames) {
n = frames - processed;
nmax = audio_stream_bytes_without_wrap(source, src) / source_frame_bytes;
n = MIN(n, nmax);
nmax = audio_stream_bytes_without_wrap(sink, dest) >> BYTES_TO_S16_SAMPLES;
n = MIN(n, nmax);
src_ch = src + sel_channel;
for (i = 0; i < n; i++) {
*dest = *src_ch;
src_ch += nch;
dest++;
}
src = audio_stream_wrap(source, src + nch * n);
dest = audio_stream_wrap(sink, dest);
processed += n;
}
}
/**
* \brief Channel selection for 16 bit, at least 2 channels data format.
* \param[in,out] dev Selector base component device.
* \param[in,out] sink Destination buffer.
* \param[in,out] source Source buffer.
* \param[in] frames Number of frames to process.
*/
static void sel_s16le_nch(struct comp_dev *dev, struct audio_stream *sink,
const struct audio_stream *source, uint32_t frames)
{
int8_t *src = audio_stream_get_rptr(source);
int8_t *dst = audio_stream_get_wptr(sink);
int bmax;
int b;
int bytes_copied = 0;
const int bytes_total = frames * audio_stream_frame_bytes(source);
while (bytes_copied < bytes_total) {
b = bytes_total - bytes_copied;
bmax = audio_stream_bytes_without_wrap(source, src);
b = MIN(b, bmax);
bmax = audio_stream_bytes_without_wrap(sink, dst);
b = MIN(b, bmax);
memcpy_s(dst, b, src, b);
src = audio_stream_wrap(source, src + b);
dst = audio_stream_wrap(sink, dst + b);
bytes_copied += b;
}
}
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE
/**
* \brief Channel selection for 32 bit, 1 channel data format.
* \param[in,out] dev Selector base component device.
* \param[in,out] sink Destination buffer.
* \param[in,out] source Source buffer.
* \param[in] frames Number of frames to process.
*/
static void sel_s32le_1ch(struct comp_dev *dev, struct audio_stream *sink,
const struct audio_stream *source, uint32_t frames)
{
struct comp_data *cd = comp_get_drvdata(dev);
int32_t *src = audio_stream_get_rptr(source);
int32_t *dest = audio_stream_get_wptr(sink);
int32_t *src_ch;
int nmax;
int i;
int n;
int processed = 0;
const int source_frame_bytes = audio_stream_frame_bytes(source);
const unsigned int nch = audio_stream_get_channels(source);
const unsigned int sel_channel = cd->config.sel_channel; /* 0 to nch - 1 */
while (processed < frames) {
n = frames - processed;
nmax = audio_stream_bytes_without_wrap(source, src) / source_frame_bytes;
n = MIN(n, nmax);
nmax = audio_stream_bytes_without_wrap(sink, dest) >> BYTES_TO_S32_SAMPLES;
n = MIN(n, nmax);
src_ch = src + sel_channel;
for (i = 0; i < n; i++) {
*dest = *src_ch;
src_ch += nch;
dest++;
}
src = audio_stream_wrap(source, src + nch * n);
dest = audio_stream_wrap(sink, dest);
processed += n;
}
}
/**
* \brief Channel selection for 32 bit, at least 2 channels data format.
* \param[in,out] dev Selector base component device.
* \param[in,out] sink Destination buffer.
* \param[in,out] source Source buffer.
* \param[in] frames Number of frames to process.
*/
static void sel_s32le_nch(struct comp_dev *dev, struct audio_stream *sink,
const struct audio_stream *source, uint32_t frames)
{
int8_t *src = audio_stream_get_rptr(source);
int8_t *dst = audio_stream_get_wptr(sink);
int bmax;
int b;
int bytes_copied = 0;
const int bytes_total = frames * audio_stream_frame_bytes(source);
while (bytes_copied < bytes_total) {
b = bytes_total - bytes_copied;
bmax = audio_stream_bytes_without_wrap(source, src);
b = MIN(b, bmax);
bmax = audio_stream_bytes_without_wrap(sink, dst);
b = MIN(b, bmax);
memcpy_s(dst, b, src, b);
src = audio_stream_wrap(source, src + b);
dst = audio_stream_wrap(sink, dst + b);
bytes_copied += b;
}
}
#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */
#else
#if CONFIG_FORMAT_S16LE
/**
* \brief Mixing routine for 16-bit, m channel input x n channel output single frame.
* \param[out] dst Sink buffer.
* \param[in] dst_channels Number of sink channels.
* \param[in] src Source data.
* \param[in] src_channels Number of source channels.
* \param[in] coeffs_config IPC4 micsel config with Q10 coefficients.
*/
static void process_frame_s16le(int16_t dst[], int dst_channels,
int16_t src[], int src_channels,
struct ipc4_selector_coeffs_config *coeffs_config)
{
int32_t accum;
int i, j;
for (i = 0; i < dst_channels; i++) {
accum = 0;
for (j = 0; j < src_channels; j++)
accum += (int32_t)src[j] * (int32_t)coeffs_config->coeffs[i][j];
/* shift out 10 LSbits with rounding to get 16-bit result */
dst[i] = (int16_t)((accum + (1 << 9)) >> 10);
}
}
/**
* \brief Channel selection for 16-bit, m channel input x n channel output data format.
* \param[in] mod Selector base module device.
* \param[in,out] bsource Source buffer.
* \param[in,out] bsink Sink buffer.
* \param[in] frames Number of frames to process.
*/
static void sel_s16le(struct processing_module *mod, struct input_stream_buffer *bsource,
struct output_stream_buffer *bsink, uint32_t frames)
{
struct comp_data *cd = module_get_private_data(mod);
struct audio_stream *source = bsource->data;
struct audio_stream *sink = bsink->data;
int16_t *src = audio_stream_get_rptr(source);
int16_t *dest = audio_stream_get_wptr(sink);
int nmax;
int i;
int n;
int processed = 0;
int source_frame_bytes = audio_stream_frame_bytes(source);
int sink_frame_bytes = audio_stream_frame_bytes(sink);
int n_chan_source = MIN(SEL_SOURCE_CHANNELS_MAX, audio_stream_get_channels(source));
int n_chan_sink = MIN(SEL_SINK_CHANNELS_MAX, audio_stream_get_channels(sink));
while (processed < frames) {
n = frames - processed;
nmax = audio_stream_bytes_without_wrap(source, src) / source_frame_bytes;
n = MIN(n, nmax);
nmax = audio_stream_bytes_without_wrap(sink, dest) / sink_frame_bytes;
n = MIN(n, nmax);
for (i = 0; i < n; i++) {
process_frame_s16le(dest, n_chan_sink, src, n_chan_source,
&cd->coeffs_config);
src += audio_stream_get_channels(source);
dest += audio_stream_get_channels(sink);
}
src = audio_stream_wrap(source, src);
dest = audio_stream_wrap(sink, dest);
processed += n;
}
module_update_buffer_position(bsource, bsink, frames);
}
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE
/**
* \brief Mixing routine for 32-bit, m channel input x n channel output single frame.
* \param[out] dst Sink buffer.
* \param[in] dst_channels Number of sink channels.
* \param[in] src Source data.
* \param[in] src_channels Number of source channels.
* \param[in] coeffs_config IPC4 micsel config with Q10 coefficients.
*/
static void process_frame_s32le(int32_t dst[], int dst_channels,
int32_t src[], int src_channels,
struct ipc4_selector_coeffs_config *coeffs_config)
{
int64_t accum;
int i, j;
for (i = 0; i < dst_channels; i++) {
accum = 0;
for (j = 0; j < src_channels; j++)
accum += (int64_t)src[j] * (int64_t)coeffs_config->coeffs[i][j];
/* shift out 10 LSbits with rounding to get 32-bit result */
dst[i] = (int32_t)((accum + (1 << 9)) >> 10);
}
}
/**
* \brief Channel selection for 32-bit, m channel input x n channel output data format.
* \param[in] mod Selector base module device.
* \param[in,out] bsource Source buffer.
* \param[in,out] bsink Sink buffer.
* \param[in] frames Number of frames to process.
*/
static void sel_s32le(struct processing_module *mod, struct input_stream_buffer *bsource,
struct output_stream_buffer *bsink, uint32_t frames)
{
struct comp_data *cd = module_get_private_data(mod);
struct audio_stream *source = bsource->data;
struct audio_stream *sink = bsink->data;
int32_t *src = audio_stream_get_rptr(source);
int32_t *dest = audio_stream_get_wptr(sink);
int nmax;
int i;
int n;
int processed = 0;
int source_frame_bytes = audio_stream_frame_bytes(source);
int sink_frame_bytes = audio_stream_frame_bytes(sink);
int n_chan_source = MIN(SEL_SOURCE_CHANNELS_MAX, audio_stream_get_channels(source));
int n_chan_sink = MIN(SEL_SINK_CHANNELS_MAX, audio_stream_get_channels(sink));
while (processed < frames) {
n = frames - processed;
nmax = audio_stream_bytes_without_wrap(source, src) / source_frame_bytes;
n = MIN(n, nmax);
nmax = audio_stream_bytes_without_wrap(sink, dest) / sink_frame_bytes;
n = MIN(n, nmax);
for (i = 0; i < n; i++) {
process_frame_s32le(dest, n_chan_sink, src, n_chan_source,
&cd->coeffs_config);
src += audio_stream_get_channels(source);
dest += audio_stream_get_channels(sink);
}
src = audio_stream_wrap(source, src);
dest = audio_stream_wrap(sink, dest);
processed += n;
}
module_update_buffer_position(bsource, bsink, frames);
}
#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */
#endif
const struct comp_func_map func_table[] = {
#if CONFIG_IPC_MAJOR_3
#if CONFIG_FORMAT_S16LE
{SOF_IPC_FRAME_S16_LE, 1, sel_s16le_1ch},
{SOF_IPC_FRAME_S16_LE, 2, sel_s16le_nch},
{SOF_IPC_FRAME_S16_LE, 4, sel_s16le_nch},
#endif /* CONFIG_FORMAT_S16LE */
#if CONFIG_FORMAT_S24LE
{SOF_IPC_FRAME_S24_4LE, 1, sel_s32le_1ch},
{SOF_IPC_FRAME_S24_4LE, 2, sel_s32le_nch},
{SOF_IPC_FRAME_S24_4LE, 4, sel_s32le_nch},
#endif /* CONFIG_FORMAT_S24LE */
#if CONFIG_FORMAT_S32LE
{SOF_IPC_FRAME_S32_LE, 1, sel_s32le_1ch},
{SOF_IPC_FRAME_S32_LE, 2, sel_s32le_nch},
{SOF_IPC_FRAME_S32_LE, 4, sel_s32le_nch},
#endif /* CONFIG_FORMAT_S32LE */
#else
#if CONFIG_FORMAT_S16LE
{SOF_IPC_FRAME_S16_LE, 0, sel_s16le},
#endif
#if CONFIG_FORMAT_S24LE
{SOF_IPC_FRAME_S24_4LE, 0, sel_s32le},
#endif
#if CONFIG_FORMAT_S32LE
{SOF_IPC_FRAME_S32_LE, 0, sel_s32le},
#endif
#endif
};
#if CONFIG_IPC_MAJOR_3
sel_func sel_get_processing_function(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
int i;
/* map the channel selection function for source and sink buffers */
for (i = 0; i < ARRAY_SIZE(func_table); i++) {
if (cd->source_format != func_table[i].source)
continue;
if (cd->config.out_channels_count != func_table[i].out_channels)
continue;
/* TODO: add additional criteria as needed */
return func_table[i].sel_func;
}
return NULL;
}
#else
sel_func sel_get_processing_function(struct processing_module *mod)
{
struct comp_data *cd = module_get_private_data(mod);
int i;
/* map the channel selection function for source and sink buffers */
for (i = 0; i < ARRAY_SIZE(func_table); i++) {
if (cd->source_format != func_table[i].source)
continue;
/* TODO: add additional criteria as needed */
return func_table[i].sel_func;
}
return NULL;
}
#endif