forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdts.c
More file actions
470 lines (373 loc) · 12.9 KB
/
dts.c
File metadata and controls
470 lines (373 loc) · 12.9 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2021 Xperi. All rights reserved.
//
// Author: Mark Barton <mark.barton@xperi.com>
#include <rtos/init.h>
#include <sof/audio/module_adapter/module/generic.h>
#include "DtsSofInterface.h"
LOG_MODULE_REGISTER(dts, CONFIG_SOF_LOG_LEVEL);
SOF_DEFINE_REG_UUID(dts);
DECLARE_TR_CTX(dts_tr, SOF_UUID(dts_uuid), LOG_LEVEL_INFO);
#define MAX_EXPECTED_DTS_CONFIG_DATA_SIZE 8192
static void *dts_effect_allocate_codec_memory(void *mod_void, unsigned int length,
unsigned int alignment)
{
struct processing_module *mod = mod_void;
struct comp_dev *dev = mod->dev;
void *pMem;
comp_dbg(dev, "dts_effect_allocate_codec_memory() start");
pMem = module_allocate_memory(mod, (uint32_t)length, (uint32_t)alignment);
if (pMem == NULL)
comp_err(dev,
"dts_effect_allocate_codec_memory() failed to allocate %d bytes", length);
comp_dbg(dev, "dts_effect_allocate_codec_memory() done");
return pMem;
}
static void dts_effect_free_codec_memory(void *mod_void, void *pMem)
{
struct processing_module *mod = mod_void;
struct comp_dev *dev = mod->dev;
comp_dbg(dev, "dts_effect_free_codec_memory() start");
int ret = module_free_memory(mod, pMem);
if (ret)
comp_err(dev, "dts_effect_free_codec_memory() module_free_memory failed %d", ret);
comp_dbg(dev, "dts_effect_free_codec_memory() done");
}
static int dts_effect_convert_sof_interface_result(struct comp_dev *dev,
DtsSofInterfaceResult dts_result)
{
int ret;
switch (dts_result) {
case DTS_SOF_INTERFACE_RESULT_SUCCESS:
ret = 0;
break;
case DTS_SOF_INTERFACE_RESULT_ERROR_NO_MEMORY:
ret = -ENOMEM;
break;
case DTS_SOF_INTERFACE_RESULT_ERROR_DTS_INTERNAL_MODULE_ERROR:
ret = -EIO;
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static int dts_effect_populate_buffer_configuration(struct comp_dev *dev,
DtsSofInterfaceBufferConfiguration *buffer_config)
{
struct comp_buffer *source = comp_dev_get_first_data_producer(dev);
const struct audio_stream *stream;
DtsSofInterfaceBufferLayout buffer_layout;
DtsSofInterfaceBufferFormat buffer_format;
unsigned int buffer_fmt, frame_fmt, rate, channels;
comp_dbg(dev, "dts_effect_populate_buffer_configuration() start");
if (!source)
return -EINVAL;
stream = &source->stream;
buffer_fmt = audio_stream_get_buffer_fmt(stream);
frame_fmt = audio_stream_get_frm_fmt(stream);
rate = audio_stream_get_rate(stream);
channels = audio_stream_get_channels(stream);
switch (buffer_fmt) {
case SOF_IPC_BUFFER_INTERLEAVED:
buffer_layout = DTS_SOF_INTERFACE_BUFFER_LAYOUT_INTERLEAVED;
break;
case SOF_IPC_BUFFER_NONINTERLEAVED:
buffer_layout = DTS_SOF_INTERFACE_BUFFER_LAYOUT_NONINTERLEAVED;
break;
default:
return -EINVAL;
}
switch (frame_fmt) {
case SOF_IPC_FRAME_S16_LE:
buffer_format = DTS_SOF_INTERFACE_BUFFER_FORMAT_SINT16LE;
break;
case SOF_IPC_FRAME_S24_4LE:
buffer_format = DTS_SOF_INTERFACE_BUFFER_FORMAT_SINT24LE;
break;
case SOF_IPC_FRAME_S32_LE:
buffer_format = DTS_SOF_INTERFACE_BUFFER_FORMAT_SINT32LE;
break;
case SOF_IPC_FRAME_FLOAT:
buffer_format = DTS_SOF_INTERFACE_BUFFER_FORMAT_FLOAT32;
break;
default:
return -EINVAL;
}
buffer_config->bufferLayout = buffer_layout;
buffer_config->bufferFormat = buffer_format;
buffer_config->sampleRate = rate;
buffer_config->numChannels = channels;
buffer_config->periodInFrames = dev->frames;
comp_dbg(dev, "dts_effect_populate_buffer_configuration() done");
return 0;
}
static int dts_codec_init(struct processing_module *mod)
{
int ret;
struct comp_dev *dev = mod->dev;
struct module_data *codec = &mod->priv;
DtsSofInterfaceResult dts_result;
DtsSofInterfaceVersionInfo interface_version;
DtsSofInterfaceVersionInfo sdk_version;
comp_dbg(dev, "dts_codec_init() start");
dts_result = dtsSofInterfaceInit((DtsSofInterfaceInst **)&(codec->private),
dts_effect_allocate_codec_memory, dts_effect_free_codec_memory, mod);
ret = dts_effect_convert_sof_interface_result(dev, dts_result);
if (ret)
comp_err(dev, "dts_codec_init() dtsSofInterfaceInit failed %d %d", ret, dts_result);
/* Obtain the current versions of DTS interface and SDK */
dts_result = dtsSofInterfaceGetVersion(&interface_version, &sdk_version);
/* Not necessary to fail initialisation if only get version failed */
if (dts_result == DTS_SOF_INTERFACE_RESULT_SUCCESS) {
comp_info(dev,
"dts_codec_init() DTS SOF Interface version %d.%d.%d.%d",
interface_version.major,
interface_version.minor,
interface_version.patch,
interface_version.build);
comp_info(dev,
"dts_codec_init() DTS SDK version %d.%d.%d.%d",
sdk_version.major,
sdk_version.minor,
sdk_version.patch,
sdk_version.build);
}
if (ret)
comp_err(dev, "dts_codec_init() failed %d %d", ret, dts_result);
comp_dbg(dev, "dts_codec_init() done");
return ret;
}
static int dts_codec_prepare(struct processing_module *mod,
struct sof_source **sources, int num_of_sources,
struct sof_sink **sinks, int num_of_sinks)
{
int ret;
struct comp_dev *dev = mod->dev;
struct module_data *codec = &mod->priv;
DtsSofInterfaceBufferConfiguration buffer_configuration;
DtsSofInterfaceResult dts_result;
comp_dbg(dev, "dts_codec_prepare() start");
ret = dts_effect_populate_buffer_configuration(dev, &buffer_configuration);
if (ret) {
comp_err(dev,
"dts_codec_prepare() dts_effect_populate_buffer_configuration failed %d",
ret);
return ret;
}
dts_result = dtsSofInterfacePrepare(
(DtsSofInterfaceInst *)codec->private,
&buffer_configuration,
&codec->mpd.in_buff,
&codec->mpd.in_buff_size,
&codec->mpd.out_buff,
&codec->mpd.out_buff_size);
ret = dts_effect_convert_sof_interface_result(dev, dts_result);
if (ret)
comp_err(dev, "dts_codec_prepare() failed %d", ret);
comp_dbg(dev, "dts_codec_prepare() done");
return ret;
}
static int dts_codec_init_process(struct processing_module *mod)
{
int ret;
struct comp_dev *dev = mod->dev;
struct module_data *codec = &mod->priv;
DtsSofInterfaceResult dts_result;
comp_dbg(dev, "dts_codec_init_process() start");
dts_result = dtsSofInterfaceInitProcess(codec->private);
ret = dts_effect_convert_sof_interface_result(dev, dts_result);
codec->mpd.produced = 0;
codec->mpd.consumed = 0;
codec->mpd.init_done = 1;
if (ret)
comp_err(dev, "dts_codec_init_process() failed %d %d", ret, dts_result);
comp_dbg(dev, "dts_codec_init_process() done");
return ret;
}
static int
dts_codec_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)
{
int ret;
struct comp_dev *dev = mod->dev;
struct module_data *codec = &mod->priv;
DtsSofInterfaceResult dts_result;
unsigned int bytes_processed = 0;
/* Proceed only if we have enough data to fill the module buffer completely */
if (input_buffers[0].size < codec->mpd.in_buff_size) {
comp_dbg(dev, "dts_codec_process(): not enough data to process");
return -ENODATA;
}
if (!codec->mpd.init_done) {
ret = dts_codec_init_process(mod);
if (ret < 0)
return ret;
}
memcpy_s(codec->mpd.in_buff, codec->mpd.in_buff_size,
input_buffers[0].data, codec->mpd.in_buff_size);
codec->mpd.avail = codec->mpd.in_buff_size;
comp_dbg(dev, "dts_codec_process() start");
dts_result = dtsSofInterfaceProcess(codec->private, &bytes_processed);
ret = dts_effect_convert_sof_interface_result(dev, dts_result);
codec->mpd.consumed = !ret ? bytes_processed : 0;
codec->mpd.produced = !ret ? bytes_processed : 0;
input_buffers[0].consumed = codec->mpd.consumed;
if (ret) {
comp_err(dev, "dts_codec_process() failed %d %d", ret, dts_result);
return ret;
}
/* copy the produced samples into the output buffer */
memcpy_s(output_buffers[0].data, codec->mpd.produced, codec->mpd.out_buff,
codec->mpd.produced);
output_buffers[0].size = codec->mpd.produced;
comp_dbg(dev, "dts_codec_process() done");
return ret;
}
static int dts_codec_apply_config(struct processing_module *mod)
{
int ret = 0;
struct comp_dev *dev = mod->dev;
struct module_data *codec = &mod->priv;
struct module_config *config;
struct module_param *param;
uint32_t config_header_size;
uint32_t config_data_size;
uint32_t param_header_size;
uint32_t param_data_size;
uint32_t i;
uint32_t param_number = 0;
DtsSofInterfaceResult dts_result;
comp_dbg(dev, "dts_codec_apply_config() start");
config = &codec->cfg;
/* Check that config->data isn't invalid and has size greater than 0 */
config_header_size = sizeof(config->size) + sizeof(config->avail);
if (config->size < config_header_size) {
comp_warn(dev, "dts_codec_apply_config() config->data is invalid");
return 0;
} else if (config->size == config_header_size) {
comp_warn(dev, "dts_codec_apply_config() size of config->data is 0");
return 0;
}
/* Calculate size of config->data */
config_data_size = config->size - config_header_size;
/* Check that config->data is not greater than the max expected for DTS data */
if (config_data_size > MAX_EXPECTED_DTS_CONFIG_DATA_SIZE) {
comp_err(dev,
"dts_codec_apply_config() size of config->data is larger than max for DTS data");
return -EINVAL;
}
/* Allow for multiple module_params to be packed into the data pointed to by config
*/
for (i = 0; i < config_data_size; param_number++) {
param = (struct module_param *)((char *)config->data + i);
param_header_size = sizeof(param->id) + sizeof(param->size);
/* If param->size is less than param_header_size, then this param is not valid */
if (param->size < param_header_size) {
comp_err(dev, "dts_codec_apply_config() param is invalid");
return -EINVAL;
}
/* Only process param->data if it has size greater than 0 */
if (param->size > param_header_size) {
/* Calculate size of param->data */
param_data_size = param->size - param_header_size;
comp_dbg(dev, "dts_codec_apply_config() id %d size %d",
param->id, param_data_size);
if (param_data_size) {
dts_result = dtsSofInterfaceApplyConfig(codec->private, param->id,
param->data, param_data_size);
ret = dts_effect_convert_sof_interface_result(dev, dts_result);
if (ret) {
comp_err(dev,
"dts_codec_apply_config() dtsSofInterfaceApplyConfig failed %d",
dts_result);
return ret;
}
}
}
/* Advance to the next module_param */
i += param->size;
}
comp_dbg(dev, "dts_codec_apply_config() done");
return ret;
}
static int dts_codec_reset(struct processing_module *mod)
{
int ret;
struct comp_dev *dev = mod->dev;
struct module_data *codec = &mod->priv;
DtsSofInterfaceResult dts_result;
comp_dbg(dev, "dts_codec_reset() start");
dts_result = dtsSofInterfaceReset(codec->private);
ret = dts_effect_convert_sof_interface_result(dev, dts_result);
if (ret)
comp_err(dev, "dts_codec_reset() failed %d %d", ret, dts_result);
comp_dbg(dev, "dts_codec_reset() done");
return ret;
}
static int dts_codec_free(struct processing_module *mod)
{
int ret;
struct comp_dev *dev = mod->dev;
struct module_data *codec = &mod->priv;
DtsSofInterfaceResult dts_result;
comp_dbg(dev, "dts_codec_free() start");
dts_result = dtsSofInterfaceFree(codec->private);
ret = dts_effect_convert_sof_interface_result(dev, dts_result);
if (ret)
comp_err(dev, "dts_codec_free() failed %d %d", ret, dts_result);
module_free_all_memory(mod);
comp_dbg(dev, "dts_codec_free() done");
return ret;
}
static int
dts_codec_set_configuration(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 module_data *md = &mod->priv;
struct comp_dev *dev = mod->dev;
int ret;
ret = module_set_configuration(mod, config_id, pos, data_offset_size, fragment,
fragment_size, response, response_size);
if (ret < 0) {
comp_err(dev, "dts_codec_set_configuration(): error %x from module_set_configuration()",
ret);
return ret;
}
/* return if more fragments are expected */
if (pos != MODULE_CFG_FRAGMENT_LAST && pos != MODULE_CFG_FRAGMENT_SINGLE) {
comp_err(dev, "dts_codec_set_configuration(): pos %d error", pos);
return 0;
}
#if CONFIG_IPC_MAJOR_3
// return if the module is not prepared
if (md->state < MODULE_INITIALIZED) {
comp_err(dev, "dts_codec_set_configuration(): state %d error", md->state);
return 0;
}
#endif
/* whole configuration received, apply it now */
ret = dts_codec_apply_config(mod);
if (ret) {
comp_err(dev, "dts_codec_set_configuration(): error %x: runtime config apply failed",
ret);
return ret;
}
comp_dbg(dev, "dts_codec_set_configuration(): config applied");
return 0;
}
static const struct module_interface dts_interface = {
.init = dts_codec_init,
.prepare = dts_codec_prepare,
.process_raw_data = dts_codec_process,
.set_configuration = dts_codec_set_configuration,
.reset = dts_codec_reset,
.free = dts_codec_free
};
DECLARE_MODULE_ADAPTER(dts_interface, dts_uuid, dts_tr);
SOF_MODULE_INIT(dts, sys_comp_module_dts_interface_init);