Skip to content

Commit cfff6ef

Browse files
committed
module: cadence: rework module to use sink/source api
Rework the cadence ipc3 module variant to only use the sink/source api to prepare sof for the full transition to pipeline 2.0. Make cadence_copy_data_from_buffer() shared across both ipc3 and ipc4 variants. Add a common cadence_copy_data_to_buffer() function. Remove the unnecessary const modifier from sink_buffer_start and eliminate redundant type casts. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent c351193 commit cfff6ef

4 files changed

Lines changed: 98 additions & 67 deletions

File tree

src/audio/module_adapter/module/cadence.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,40 @@ int cadence_codec_process_data(struct processing_module *mod,
573573

574574
return 0;
575575
}
576+
577+
void cadence_copy_data_from_buffer(void *dest, const void *buffer_ptr, size_t bytes_to_copy,
578+
size_t buffer_size, void const *buffer_start)
579+
{
580+
size_t bytes_to_end = (size_t)((const uint8_t *)buffer_start +
581+
buffer_size - (const uint8_t *)buffer_ptr);
582+
583+
if (bytes_to_end >= bytes_to_copy) {
584+
/* No wrap, copy directly */
585+
memcpy_s(dest, bytes_to_copy, buffer_ptr, bytes_to_copy);
586+
return;
587+
}
588+
589+
/* Wrap occurs, copy in two parts */
590+
memcpy_s(dest, bytes_to_end, buffer_ptr, bytes_to_end);
591+
memcpy_s((uint8_t *)dest + bytes_to_end, bytes_to_copy - bytes_to_end,
592+
buffer_start, bytes_to_copy - bytes_to_end);
593+
}
594+
595+
void cadence_copy_data_to_buffer(void *buffer_ptr, size_t bytes_to_copy,
596+
size_t buffer_size, void *buffer_start,
597+
const void *src)
598+
{
599+
size_t bytes_to_end = (size_t)((uint8_t *)buffer_start +
600+
buffer_size - (uint8_t *)buffer_ptr);
601+
602+
if (bytes_to_end >= bytes_to_copy) {
603+
/* No wrap, copy directly */
604+
memcpy_s(buffer_ptr, bytes_to_copy, src, bytes_to_copy);
605+
return;
606+
}
607+
608+
/* Wrap occurs, copy in two parts */
609+
memcpy_s(buffer_ptr, bytes_to_end, src, bytes_to_end);
610+
memcpy_s(buffer_start, bytes_to_copy - bytes_to_end,
611+
(const uint8_t *)src + bytes_to_end, bytes_to_copy - bytes_to_end);
612+
}

src/audio/module_adapter/module/cadence_ipc3.c

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,40 +211,52 @@ static int cadence_codec_prepare(struct processing_module *mod,
211211
return ret;
212212
}
213213

214-
static int
215-
cadence_codec_process(struct processing_module *mod,
216-
struct input_stream_buffer *input_buffers, int num_input_buffers,
217-
struct output_stream_buffer *output_buffers, int num_output_buffers)
214+
static int cadence_codec_process(struct processing_module *mod,
215+
struct sof_source **sources, int num_of_sources,
216+
struct sof_sink **sinks, int num_of_sinks)
218217
{
219218
struct comp_buffer *local_buff;
220219
struct comp_dev *dev = mod->dev;
221220
struct module_data *codec = &mod->priv;
222221
int free_bytes, output_bytes = cadence_codec_get_samples(mod) *
223222
mod->stream_params->sample_container_bytes *
224223
mod->stream_params->channels;
225-
uint32_t remaining = input_buffers[0].size;
224+
size_t remaining = source_get_data_available(sources[0]);
225+
const void *source_buffer_start, *src_ptr;
226+
void *sink_ptr, *sink_buffer_start;
227+
size_t src_bytes, sink_bytes;
226228
int ret;
227229

228230
if (!cadence_codec_deep_buff_allowed(mod))
229231
mod->deep_buff_bytes = 0;
230232

231233
/* Proceed only if we have enough data to fill the module buffer completely */
232-
if (input_buffers[0].size < codec->mpd.in_buff_size) {
234+
if (remaining < codec->mpd.in_buff_size) {
233235
comp_dbg(dev, "not enough data to process");
234236
return -ENODATA;
235237
}
236238

237239
if (!codec->mpd.init_done) {
238-
memcpy_s(codec->mpd.in_buff, codec->mpd.in_buff_size, input_buffers[0].data,
239-
codec->mpd.in_buff_size);
240+
ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr,
241+
&source_buffer_start, &src_bytes);
242+
if (ret) {
243+
comp_err(dev, "cannot get data from source buffer");
244+
return ret;
245+
}
246+
247+
cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr,
248+
codec->mpd.in_buff_size, src_bytes,
249+
source_buffer_start);
240250
codec->mpd.avail = codec->mpd.in_buff_size;
241251

242252
ret = cadence_codec_init_process(mod);
243-
if (ret)
253+
if (ret) {
254+
source_release_data(sources[0], 0);
244255
return ret;
256+
}
245257

246258
remaining -= codec->mpd.consumed;
247-
input_buffers[0].consumed = codec->mpd.consumed;
259+
source_release_data(sources[0], codec->mpd.consumed);
248260
}
249261

250262
/* do not proceed with processing if not enough free space left in the local buffer */
@@ -257,25 +269,38 @@ cadence_codec_process(struct processing_module *mod,
257269
if (remaining < codec->mpd.in_buff_size)
258270
return -ENODATA;
259271

260-
memcpy_s(codec->mpd.in_buff, codec->mpd.in_buff_size,
261-
(uint8_t *)input_buffers[0].data + input_buffers[0].consumed,
262-
codec->mpd.in_buff_size);
272+
ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr, &source_buffer_start,
273+
&src_bytes);
274+
if (ret)
275+
return ret;
276+
277+
cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr,
278+
codec->mpd.in_buff_size, src_bytes,
279+
source_buffer_start);
263280
codec->mpd.avail = codec->mpd.in_buff_size;
264281

265282
comp_dbg(dev, "cadence_codec_process() start");
266283

267284
ret = cadence_codec_process_data(mod, NULL);
268-
if (ret)
285+
if (ret) {
286+
source_release_data(sources[0], 0);
269287
return ret;
288+
}
289+
290+
source_release_data(sources[0], codec->mpd.consumed);
291+
292+
ret = sink_get_buffer(sinks[0], codec->mpd.produced, &sink_ptr, &sink_buffer_start,
293+
&sink_bytes);
294+
if (ret) {
295+
comp_err(dev, "cannot get sink buffer");
296+
return ret;
297+
}
270298

271-
/* update consumed with the number of samples consumed during init */
272-
input_buffers[0].consumed += codec->mpd.consumed;
273-
codec->mpd.consumed = input_buffers[0].consumed;
299+
/* Copy the produced samples into the output buffer */
300+
cadence_copy_data_to_buffer(sink_ptr, codec->mpd.produced, sink_bytes,
301+
sink_buffer_start, codec->mpd.out_buff);
274302

275-
/* copy the produced samples into the output buffer */
276-
memcpy_s(output_buffers[0].data, codec->mpd.produced, codec->mpd.out_buff,
277-
codec->mpd.produced);
278-
output_buffers[0].size = codec->mpd.produced;
303+
sink_commit_buffer(sinks[0], codec->mpd.produced);
279304

280305
comp_dbg(dev, "cadence_codec_process() done");
281306

@@ -307,7 +332,7 @@ static int cadence_codec_reset(struct processing_module *mod)
307332
static const struct module_interface cadence_codec_interface = {
308333
.init = cadence_codec_init,
309334
.prepare = cadence_codec_prepare,
310-
.process_raw_data = cadence_codec_process,
335+
.process = cadence_codec_process,
311336
.set_configuration = cadence_codec_set_configuration,
312337
.reset = cadence_codec_reset,
313338
.free = cadence_codec_free

src/audio/module_adapter/module/cadence_ipc4.c

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -395,36 +395,17 @@ static int cadence_codec_prepare(struct processing_module *mod,
395395
return 0;
396396
}
397397

398-
static void cadence_copy_data_from_buffer(void *dest, const void *buffer_ptr, size_t bytes_to_copy,
399-
size_t buffer_size, uint8_t const *buffer_start)
400-
{
401-
size_t bytes_to_end = (size_t)((uint8_t *)buffer_start +
402-
buffer_size - (uint8_t *)buffer_ptr);
403-
404-
if (bytes_to_end >= bytes_to_copy) {
405-
/* No wrap, copy directly */
406-
memcpy_s(dest, bytes_to_copy, buffer_ptr, bytes_to_copy);
407-
return;
408-
}
409-
410-
/* Wrap occurs, copy in two parts */
411-
memcpy_s(dest, bytes_to_end, buffer_ptr, bytes_to_end);
412-
memcpy_s((uint8_t *)dest + bytes_to_end, bytes_to_copy - bytes_to_end,
413-
buffer_start, bytes_to_copy - bytes_to_end);
414-
}
415-
416398
static int cadence_codec_process(struct processing_module *mod, struct sof_source **sources,
417399
int num_of_sources, struct sof_sink **sinks, int num_of_sinks)
418400
{
419401
struct comp_dev *dev = mod->dev;
420402
struct module_data *codec = &mod->priv;
421403
size_t in_size = source_get_data_available(sources[0]);
422404
size_t out_space = sink_get_free_size(sinks[0]);
423-
uint8_t const *source_buffer_start;
424-
int consumed_during_init = 0;
405+
const void *source_buffer_start, *src_ptr;
406+
void *sink_buffer_start, *sink_ptr;
407+
size_t src_bytes, sink_bytes;
425408
uint32_t remaining = in_size;
426-
const void *src_ptr;
427-
size_t src_bytes;
428409
int ret;
429410

430411
if (!codec->mpd.init_done) {
@@ -446,7 +427,6 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc
446427

447428
remaining -= codec->mpd.consumed;
448429
source_release_data(sources[0], codec->mpd.consumed);
449-
consumed_during_init = codec->mpd.consumed;
450430
}
451431

452432
codec->mpd.consumed = 0;
@@ -456,8 +436,8 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc
456436
return -ENODATA;
457437

458438
/* Acquire data from the source buffer */
459-
ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr,
460-
(const void **)&source_buffer_start, &src_bytes);
439+
ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr, &source_buffer_start,
440+
&src_bytes);
461441

462442
cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr, codec->mpd.in_buff_size,
463443
src_bytes, source_buffer_start);
@@ -507,32 +487,16 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc
507487
return -ENOSPC;
508488
}
509489

510-
void *sink_ptr;
511-
size_t sink_bytes;
512-
uint8_t const *sink_buffer_start;
513-
514-
ret = sink_get_buffer(sinks[0], codec->mpd.produced, &sink_ptr,
515-
(void **)&sink_buffer_start, &sink_bytes);
490+
ret = sink_get_buffer(sinks[0], codec->mpd.produced, &sink_ptr, &sink_buffer_start,
491+
&sink_bytes);
516492
if (ret) {
517493
comp_err(dev, "cannot get sink buffer");
518494
return ret;
519495
}
520496

521497
/* Copy the produced samples into the output buffer */
522-
size_t bytes_to_end = (size_t)((uint8_t *)sink_buffer_start +
523-
sink_bytes - (uint8_t *)sink_ptr);
524-
525-
if (bytes_to_end >= codec->mpd.produced) {
526-
/* No wrap, copy directly */
527-
memcpy_s(sink_ptr, codec->mpd.produced, codec->mpd.out_buff,
528-
codec->mpd.produced);
529-
} else {
530-
/* Wrap occurs, copy in two parts */
531-
memcpy_s(sink_ptr, bytes_to_end, codec->mpd.out_buff, bytes_to_end);
532-
memcpy_s((uint8_t *)sink_buffer_start, codec->mpd.produced - bytes_to_end,
533-
(uint8_t *)codec->mpd.out_buff + bytes_to_end,
534-
codec->mpd.produced - bytes_to_end);
535-
}
498+
cadence_copy_data_to_buffer(sink_ptr, codec->mpd.produced, sink_bytes,
499+
sink_buffer_start, codec->mpd.out_buff);
536500

537501
source_release_data(sources[0], codec->mpd.consumed);
538502
sink_commit_buffer(sinks[0], codec->mpd.produced);

src/include/sof/audio/module_adapter/module/cadence.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,10 @@ int cadence_init_codec_object(struct processing_module *mod);
107107
int cadence_codec_resolve_api(struct processing_module *mod);
108108
int cadence_codec_free(struct processing_module *mod);
109109
size_t cadence_api_table_size(void);
110+
void cadence_copy_data_from_buffer(void *dest, const void *buffer_ptr, size_t bytes_to_copy,
111+
size_t buffer_size, void const *buffer_start);
112+
void cadence_copy_data_to_buffer(void *buffer_ptr, size_t bytes_to_copy,
113+
size_t buffer_size, void *buffer_start,
114+
const void *src);
110115

111116
#endif /* __SOF_AUDIO_CADENCE_CODEC__ */

0 commit comments

Comments
 (0)