Repository navigation
Expand file tree
/
Copy pathselector_generic.c
More file actions
205 lines (188 loc) · 6.06 KB
/
Copy pathselector_generic.c
File metadata and controls
205 lines (188 loc) · 6.06 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
// 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_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 = source->r_ptr;
int16_t *dest = sink->w_ptr;
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 = source->channels;
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 = (int8_t *)source->r_ptr;
int8_t *dst = (int8_t *)sink->w_ptr;
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(dst, 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 = source->r_ptr;
int32_t *dest = sink->w_ptr;
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 = source->channels;
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 = (int8_t *)source->r_ptr;
int8_t *dst = (int8_t *)sink->w_ptr;
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(dst, 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 */
const struct comp_func_map func_table[] = {
#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 */
};
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;
}