Repository navigation
Expand file tree
/
Copy pathdcblock.c
More file actions
422 lines (345 loc) · 10.1 KB
/
Copy pathdcblock.c
File metadata and controls
422 lines (345 loc) · 10.1 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2020 Google LLC. All rights reserved.
//
// Author: Sebastiano Carlucci <scarlucci@google.com>
#include <sof/audio/buffer.h>
#include <sof/audio/component.h>
#include <sof/audio/format.h>
#include <sof/audio/pipeline.h>
#include <sof/audio/dcblock/dcblock.h>
#include <sof/audio/ipc-config.h>
#include <sof/common.h>
#include <sof/debug/panic.h>
#include <sof/ipc/msg.h>
#include <sof/lib/alloc.h>
#include <sof/lib/memory.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <sof/platform.h>
#include <sof/string.h>
#include <sof/ut.h>
#include <sof/trace/trace.h>
#include <ipc/control.h>
#include <ipc/stream.h>
#include <ipc/topology.h>
#include <user/trace.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
static const struct comp_driver comp_dcblock;
LOG_MODULE_REGISTER(dcblock, CONFIG_SOF_LOG_LEVEL);
/* b809efaf-5681-42b1-9ed6-04bb012dd384 */
DECLARE_SOF_RT_UUID("dcblock", dcblock_uuid, 0xb809efaf, 0x5681, 0x42b1,
0x9e, 0xd6, 0x04, 0xbb, 0x01, 0x2d, 0xd3, 0x84);
DECLARE_TR_CTX(dcblock_tr, SOF_UUID(dcblock_uuid), LOG_LEVEL_INFO);
/**
* \brief Sets the DC Blocking filter in pass through mode.
* The frequency response of a DCB filter is:
* H(z) = (1 - z^-1)/(1-Rz^-1).
* Setting R to 1 makes the filter act as a passthrough component.
*/
static void dcblock_set_passthrough(struct comp_data *cd)
{
comp_cl_info(&comp_dcblock, "dcblock_set_passthrough()");
int i;
for (i = 0; i < PLATFORM_MAX_CHANNELS; i++)
cd->R_coeffs[i] = ONE_Q2_30;
}
/**
* \brief Initializes the state of the DC Blocking Filter
*/
static void dcblock_init_state(struct comp_data *cd)
{
memset(cd->state, 0, sizeof(cd->state));
}
/**
* \brief Creates DC Blocking Filter component.
* \return Pointer to DC Blocking Filter component device.
*/
static struct comp_dev *dcblock_new(const struct comp_driver *drv,
struct comp_ipc_config *config,
void *spec)
{
struct comp_dev *dev;
struct comp_data *cd;
struct ipc_config_process *ipc_dcblock = spec;
size_t bs = ipc_dcblock->size;
int ret;
comp_cl_info(&comp_dcblock, "dcblock_new()");
dev = comp_alloc(drv, sizeof(*dev));
if (!dev)
return NULL;
dev->ipc_config = *config;
cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
if (!cd) {
rfree(dev);
return NULL;
}
comp_set_drvdata(dev, cd);
cd->dcblock_func = NULL;
/**
* Copy over the coefficients from the blob to cd->R_coeffs
* Set passthrough if the size of the blob is invalid
*/
if (bs == sizeof(cd->R_coeffs)) {
ret = memcpy_s(cd->R_coeffs, bs, ipc_dcblock->data, bs);
assert(!ret);
} else {
if (bs > 0)
comp_cl_warn(&comp_dcblock, "dcblock_new(), binary blob size %i, expected %i",
bs, sizeof(cd->R_coeffs));
dcblock_set_passthrough(cd);
}
dev->state = COMP_STATE_READY;
return dev;
}
/**
* \brief Frees DC Blocking Filter component.
* \param[in,out] dev DC Blocking Filter base component device.
*/
static void dcblock_free(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
comp_info(dev, "dcblock_free()");
rfree(cd);
rfree(dev);
}
static int dcblock_verify_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
int ret;
comp_dbg(dev, "dcblock_verify_params()");
ret = comp_verify_params(dev, 0, params);
if (ret < 0) {
comp_err(dev, "dcblock_verify_params() error: comp_verify_params() failed.");
return ret;
}
return 0;
}
/**
* \brief Sets DC Blocking Filter component audio stream parameters.
* \param[in,out] dev DC Blocking Filter base component device.
* \return Error code.
*
* All done in prepare() since we need to know source and sink component params.
*/
static int dcblock_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
int err;
comp_dbg(dev, "dcblock_params()");
err = dcblock_verify_params(dev, params);
if (err < 0) {
comp_err(dev, "dcblock_params(): pcm params verification failed");
return -EINVAL;
}
return 0;
}
static int dcblock_cmd_get_data(struct comp_dev *dev,
struct sof_ipc_ctrl_data *cdata,
size_t max_size)
{
struct comp_data *cd = comp_get_drvdata(dev);
size_t resp_size;
int ret = 0;
switch (cdata->cmd) {
case SOF_CTRL_CMD_BINARY:
comp_info(dev, "dcblock_cmd_get_data(), SOF_CTRL_CMD_BINARY");
/* Copy coefficients back to user space */
resp_size = sizeof(cd->R_coeffs);
comp_info(dev, "dcblock_cmd_get_data(), resp_size %u",
resp_size);
if (resp_size > max_size) {
comp_err(dev, "response size %i exceeds maximum size %i ",
resp_size, max_size);
ret = -EINVAL;
break;
}
ret = memcpy_s(cdata->data->data, cdata->data->size,
cd->R_coeffs, resp_size);
assert(!ret);
cdata->data->abi = SOF_ABI_VERSION;
cdata->data->size = resp_size;
break;
default:
comp_err(dev, "dcblock_cmd_get_data(), invalid command");
ret = -EINVAL;
}
return ret;
}
static int dcblock_cmd_set_data(struct comp_dev *dev,
struct sof_ipc_ctrl_data *cdata)
{
struct comp_data *cd = comp_get_drvdata(dev);
size_t req_size = sizeof(cd->R_coeffs);
int ret = 0;
switch (cdata->cmd) {
case SOF_CTRL_CMD_BINARY:
comp_info(dev, "dcblock_cmd_set_data(), SOF_CTRL_CMD_BINARY");
/* Retrieve the binary controls from the packet */
ret = memcpy_s(cd->R_coeffs, req_size, cdata->data->data,
req_size);
assert(!ret);
break;
default:
comp_err(dev, "dcblock_set_data(), invalid command %i",
cdata->cmd);
ret = -EINVAL;
}
return ret;
}
/**
* \brief Handles incoming IPC commands for DC Blocking Filter component.
*/
static int dcblock_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_info(dev, "dcblock_cmd()");
switch (cmd) {
case COMP_CMD_SET_DATA:
ret = dcblock_cmd_set_data(dev, cdata);
break;
case COMP_CMD_GET_DATA:
ret = dcblock_cmd_get_data(dev, cdata, max_data_size);
break;
default:
comp_err(dev, "dcblock_cmd(), invalid command (%i)", cmd);
ret = -EINVAL;
}
return ret;
}
/**
* \brief Sets DC Blocking Filter component state.
* \param[in,out] dev DC Blocking Filter base component device.
* \param[in] cmd Command type.
* \return Error code.
*/
static int dcblock_trigger(struct comp_dev *dev, int cmd)
{
comp_info(dev, "dcblock_trigger()");
return comp_set_state(dev, cmd);
}
static void dcblock_process(struct comp_dev *dev, struct comp_buffer *source,
struct comp_buffer *sink, int frames,
uint32_t source_bytes, uint32_t sink_bytes)
{
struct comp_data *cd = comp_get_drvdata(dev);
buffer_stream_invalidate(source, source_bytes);
cd->dcblock_func(dev, &source->stream, &sink->stream, frames);
buffer_stream_writeback(sink, sink_bytes);
/* calc new free and available */
comp_update_buffer_consume(source, source_bytes);
comp_update_buffer_produce(sink, sink_bytes);
}
/**
* \brief Copies and processes stream data.
* \param[in,out] dev DC Blocking Filter base component device.
* \return Error code.
*/
static int dcblock_copy(struct comp_dev *dev)
{
struct comp_copy_limits cl;
struct comp_buffer *sourceb;
struct comp_buffer *sinkb;
comp_dbg(dev, "dcblock_copy()");
sourceb = list_first_item(&dev->bsource_list, struct comp_buffer,
sink_list);
sinkb = list_first_item(&dev->bsink_list, struct comp_buffer,
source_list);
/* Get source, sink, number of frames etc. to process. */
comp_get_copy_limits_with_lock(sourceb, sinkb, &cl);
dcblock_process(dev, sourceb, sinkb,
cl.frames, cl.source_bytes, cl.sink_bytes);
return 0;
}
/**
* \brief Prepares DC Blocking Filter component for processing.
* \param[in,out] dev DC Blocking Filter base component device.
* \return Error code.
*/
static int dcblock_prepare(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct comp_buffer *sourceb;
struct comp_buffer *sinkb;
uint32_t sink_period_bytes;
int ret;
comp_info(dev, "dcblock_prepare()");
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;
/* DC Filter component will only ever have one source and sink buffer */
sourceb = list_first_item(&dev->bsource_list,
struct comp_buffer, sink_list);
sinkb = list_first_item(&dev->bsink_list,
struct comp_buffer, source_list);
/* get source data format */
cd->source_format = sourceb->stream.frame_fmt;
/* get sink data format and period bytes */
cd->sink_format = sinkb->stream.frame_fmt;
sink_period_bytes =
audio_stream_period_bytes(&sinkb->stream, dev->frames);
if (sinkb->stream.size < sink_period_bytes) {
comp_err(dev, "dcblock_prepare(): sink buffer size %d is insufficient < %d",
sinkb->stream.size, sink_period_bytes);
ret = -ENOMEM;
goto err;
}
dcblock_init_state(cd);
cd->dcblock_func = dcblock_find_func(cd->source_format);
if (!cd->dcblock_func) {
comp_err(dev, "dcblock_prepare(), No processing function matching frames format");
ret = -EINVAL;
goto err;
}
comp_info(dev, "dcblock_prepare(), source_format=%d, sink_format=%d",
cd->source_format, cd->sink_format);
return 0;
err:
comp_set_state(dev, COMP_TRIGGER_RESET);
return ret;
}
/**
* \brief Resets DC Blocking Filter component.
* \param[in,out] dev DC Blocking Filter base component device.
* \return Error code.
*/
static int dcblock_reset(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
comp_info(dev, "dcblock_reset()");
dcblock_init_state(cd);
cd->dcblock_func = NULL;
comp_set_state(dev, COMP_TRIGGER_RESET);
return 0;
}
/** \brief DC Blocking Filter component definition. */
static const struct comp_driver comp_dcblock = {
.type = SOF_COMP_DCBLOCK,
.uid = SOF_RT_UUID(dcblock_uuid),
.tctx = &dcblock_tr,
.ops = {
.create = dcblock_new,
.free = dcblock_free,
.params = dcblock_params,
.cmd = dcblock_cmd,
.trigger = dcblock_trigger,
.copy = dcblock_copy,
.prepare = dcblock_prepare,
.reset = dcblock_reset,
},
};
static SHARED_DATA struct comp_driver_info comp_dcblock_info = {
.drv = &comp_dcblock,
};
UT_STATIC void sys_comp_dcblock_init(void)
{
comp_register(platform_shared_get(&comp_dcblock_info,
sizeof(comp_dcblock_info)));
}
DECLARE_MODULE(sys_comp_dcblock_init);