forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneric.c
More file actions
389 lines (323 loc) · 9.37 KB
/
generic.c
File metadata and controls
389 lines (323 loc) · 9.37 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
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2020 Intel Corporation. All rights reserved.
//
// Author: Marcin Rajwa <marcin.rajwa@linux.intel.com>
/*
* \file generic.c
* \brief Generic Codec API
* \author Marcin Rajwa <marcin.rajwa@linux.intel.com>
*
*/
#include <sof/audio/module_adapter/codec/generic.h>
/*****************************************************************************/
/* Local helper functions */
/*****************************************************************************/
static int validate_config(struct codec_config *cfg);
int
codec_load_config(struct comp_dev *dev, void *cfg, size_t size,
enum codec_cfg_type type)
{
int ret;
struct codec_config *dst;
struct comp_data *cd = comp_get_drvdata(dev);
struct codec_data *codec = &cd->codec;
comp_dbg(dev, "codec_load_config() start");
if (!dev || !cfg || !size) {
comp_err(dev, "codec_load_config(): wrong input params! dev %x, cfg %x size %d",
(uint32_t)dev, (uint32_t)cfg, size);
return -EINVAL;
}
dst = (type == CODEC_CFG_SETUP) ? &codec->s_cfg :
&codec->r_cfg;
if (!dst->data) {
/* No space for config available yet, allocate now */
dst->data = rballoc(0, SOF_MEM_CAPS_RAM, size);
} else if (dst->size != size) {
/* The size allocated for previous config doesn't match the new one.
* Free old container and allocate new one.
*/
rfree(dst->data);
dst->data = rballoc(0, SOF_MEM_CAPS_RAM, size);
}
if (!dst->data) {
comp_err(dev, "codec_load_config(): failed to allocate space for setup config.");
ret = -ENOMEM;
goto err;
}
ret = memcpy_s(dst->data, size, cfg, size);
assert(!ret);
ret = validate_config(dst->data);
if (ret) {
comp_err(dev, "codec_load_config(): validation of config failed!");
ret = -EINVAL;
goto err;
}
/* Config loaded, mark it as valid */
dst->size = size;
dst->avail = true;
comp_dbg(dev, "codec_load_config() done");
return ret;
err:
if (dst->data && type == CODEC_CFG_RUNTIME)
rfree(dst->data);
dst->data = NULL;
return ret;
}
int codec_init(struct comp_dev *dev, struct codec_interface *interface)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
uint32_t codec_id = cd->ca_config.codec_id;
struct codec_data *codec = &cd->codec;
comp_info(dev, "codec_init() start");
if (cd->codec.state == CODEC_INITIALIZED)
return 0;
if (cd->codec.state > CODEC_INITIALIZED)
return -EPERM;
codec->id = codec_id;
if (!interface) {
comp_err(dev, "codec_init(): could not find codec interface for codec id %x",
codec_id);
ret = -EIO;
goto out;
} else if (!interface->init || !interface->prepare || !interface->init_process ||
!interface->process || !interface->apply_config ||
!interface->reset || !interface->free) {
comp_err(dev, "codec_init(): codec %x is missing mandatory interfaces",
codec_id);
ret = -EIO;
goto out;
}
/* Assign interface */
codec->ops = interface;
/* Init memory list */
list_init(&codec->memory.mem_list);
/* Now we can proceed with codec specific initialization */
ret = codec->ops->init(dev);
if (ret) {
comp_err(dev, "codec_init() error %d: codec specific init failed, codec_id %x",
ret, codec_id);
goto out;
}
comp_info(dev, "codec_init() done");
codec->state = CODEC_INITIALIZED;
out:
return ret;
}
void *codec_allocate_memory(struct comp_dev *dev, uint32_t size,
uint32_t alignment)
{
struct codec_memory *container;
void *ptr;
struct comp_data *cd = comp_get_drvdata(dev);
if (!size) {
comp_err(dev, "codec_allocate_memory: requested allocation of 0 bytes.");
return NULL;
}
/* Allocate memory container */
container = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
sizeof(struct codec_memory));
if (!container) {
comp_err(dev, "codec_allocate_memory: failed to allocate memory container.");
return NULL;
}
/* Allocate memory for codec */
if (alignment)
ptr = rballoc_align(0, SOF_MEM_CAPS_RAM, size, alignment);
else
ptr = rballoc(0, SOF_MEM_CAPS_RAM, size);
if (!ptr) {
comp_err(dev, "codec_allocate_memory: failed to allocate memory for codec %x.",
cd->ca_config.codec_id);
return NULL;
}
/* Store reference to allocated memory */
container->ptr = ptr;
list_item_prepend(&container->mem_list, &cd->codec.memory.mem_list);
return ptr;
}
int codec_free_memory(struct comp_dev *dev, void *ptr)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct codec_memory *mem;
struct list_item *mem_list;
struct list_item *_mem_list;
if (!ptr)
return 0;
/* Find which container keeps this memory */
list_for_item_safe(mem_list, _mem_list, &cd->codec.memory.mem_list) {
mem = container_of(mem_list, struct codec_memory, mem_list);
if (mem->ptr == ptr) {
rfree(mem->ptr);
list_item_del(&mem->mem_list);
rfree(mem);
return 0;
}
}
comp_err(dev, "codec_free_memory: error: could not find memory pointed by %p",
(uint32_t)ptr);
return -EINVAL;
}
static int validate_config(struct codec_config *cfg)
{
/* TODO: validation of codec specific setup config */
return 0;
}
int codec_prepare(struct comp_dev *dev)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
uint32_t codec_id = cd->ca_config.codec_id;
struct codec_data *codec = &cd->codec;
comp_dbg(dev, "codec_prepare() start");
if (cd->codec.state == CODEC_IDLE)
return 0;
if (cd->codec.state < CODEC_INITIALIZED)
return -EPERM;
ret = codec->ops->prepare(dev);
if (ret) {
comp_err(dev, "codec_prepare() error %d: codec specific prepare failed, codec_id 0x%x",
ret, codec_id);
goto end;
}
/* After prepare is done we no longer need runtime configuration
* as it has been applied during the procedure - it is safe to
* free it.
*/
if (codec->r_cfg.data)
rfree(codec->r_cfg.data);
codec->s_cfg.avail = false;
codec->r_cfg.avail = false;
codec->r_cfg.data = NULL;
codec->state = CODEC_IDLE;
comp_dbg(dev, "codec_prepare() done");
end:
return ret;
}
int codec_init_process(struct comp_dev *dev)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
struct codec_data *codec = &cd->codec;
comp_dbg(dev, "codec_init_process() start");
ret = codec->ops->init_process(dev);
if (ret) {
comp_err(dev, "codec_init_process() error %x", ret);
goto out;
}
comp_dbg(dev, "codec_init_process() done");
out:
return ret;
}
int codec_process(struct comp_dev *dev)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
uint32_t codec_id = cd->ca_config.codec_id;
struct codec_data *codec = &cd->codec;
comp_dbg(dev, "codec_process() start");
if (cd->codec.state != CODEC_IDLE) {
comp_err(dev, "codec_prepare(): wrong state of codec %x, state %d",
cd->ca_config.codec_id, codec->state);
return -EPERM;
}
ret = codec->ops->process(dev);
if (ret) {
comp_err(dev, "codec_prepare() error %d: codec process failed for codec_id %x",
ret, codec_id);
goto out;
}
comp_dbg(dev, "codec_process() done");
out:
codec->state = CODEC_IDLE;
return ret;
}
int codec_get_samples(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct codec_data *codec = &cd->codec;
comp_dbg(dev, "codec_get_samples()");
if (codec->ops->get_samples)
return codec->ops->get_samples(dev);
return 0;
}
int codec_apply_runtime_config(struct comp_dev *dev)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
uint32_t codec_id = cd->ca_config.codec_id;
struct codec_data *codec = &cd->codec;
comp_dbg(dev, "codec_apply_config() start");
ret = codec->ops->apply_config(dev);
if (ret) {
comp_err(dev, "codec_apply_config() error %d: codec process failed for codec_id %x",
ret, codec_id);
goto out;
}
cd->codec.r_cfg.avail = false;
/* Configuration had been applied, we can free it now. */
rfree(codec->r_cfg.data);
codec->r_cfg.data = NULL;
comp_dbg(dev, "codec_apply_config() end");
out:
return ret;
}
int codec_reset(struct comp_dev *dev)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
struct codec_data *codec = &cd->codec;
ret = codec->ops->reset(dev);
if (ret) {
comp_err(dev, "codec_apply_config() error %d: codec specific .reset() failed for codec_id %x",
ret, cd->ca_config.codec_id);
return ret;
}
codec->r_cfg.avail = false;
codec->r_cfg.size = 0;
rfree(codec->r_cfg.data);
/* Codec reset itself to the initial condition after prepare()
* so let's change its state to reflect that.
*/
codec->state = CODEC_INITIALIZED;
return 0;
}
void codec_free_all_memory(struct comp_dev *dev)
{
struct comp_data *cd = comp_get_drvdata(dev);
struct codec_memory *mem;
struct list_item *mem_list;
struct list_item *_mem_list;
/* Find which container keeps this memory */
list_for_item_safe(mem_list, _mem_list, &cd->codec.memory.mem_list) {
mem = container_of(mem_list, struct codec_memory, mem_list);
rfree(mem->ptr);
list_item_del(&mem->mem_list);
rfree(mem);
}
}
int codec_free(struct comp_dev *dev)
{
int ret;
struct comp_data *cd = comp_get_drvdata(dev);
struct codec_data *codec = &cd->codec;
ret = codec->ops->free(dev);
if (ret) {
comp_warn(dev, "codec_apply_config() error %d: codec specific .free() failed for codec_id %x",
ret, cd->ca_config.codec_id);
}
/* Free all memory requested by codec */
codec_free_all_memory(dev);
/* Free all memory shared by codec_adapter & codec */
codec->s_cfg.avail = false;
codec->s_cfg.size = 0;
codec->r_cfg.avail = false;
codec->r_cfg.size = 0;
rfree(codec->r_cfg.data);
rfree(codec->s_cfg.data);
if (codec->runtime_params)
rfree(codec->runtime_params);
codec->state = CODEC_DISABLED;
return ret;
}