Skip to content

Commit 5bb7db1

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 5bb7db1

4 files changed

Lines changed: 93 additions & 56 deletions

File tree

src/audio/module_adapter/module/cadence.c

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

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

src/audio/module_adapter/module/cadence_ipc3.c

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,40 +211,53 @@ 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+
uint8_t const *source_buffer_start;
226+
const void *src_ptr;
227+
size_t src_bytes;
228+
void *sink_ptr;
229+
size_t sink_bytes;
230+
uint8_t *sink_buffer_start;
226231
int ret;
227232

228233
if (!cadence_codec_deep_buff_allowed(mod))
229234
mod->deep_buff_bytes = 0;
230235

231236
/* Proceed only if we have enough data to fill the module buffer completely */
232-
if (input_buffers[0].size < codec->mpd.in_buff_size) {
237+
if (remaining < codec->mpd.in_buff_size) {
233238
comp_dbg(dev, "not enough data to process");
234239
return -ENODATA;
235240
}
236241

237242
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);
243+
ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr,
244+
(const void **)&source_buffer_start, &src_bytes);
245+
if (ret) {
246+
comp_err(dev, "cannot get data from source buffer");
247+
return ret;
248+
}
249+
250+
cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr,
251+
codec->mpd.in_buff_size, src_bytes,
252+
source_buffer_start);
240253
codec->mpd.avail = codec->mpd.in_buff_size;
241254

242255
ret = cadence_codec_init_process(mod);
243256
if (ret)
244257
return ret;
245258

246259
remaining -= codec->mpd.consumed;
247-
input_buffers[0].consumed = codec->mpd.consumed;
260+
source_release_data(sources[0], codec->mpd.consumed);
248261
}
249262

250263
/* do not proceed with processing if not enough free space left in the local buffer */
@@ -257,25 +270,38 @@ cadence_codec_process(struct processing_module *mod,
257270
if (remaining < codec->mpd.in_buff_size)
258271
return -ENODATA;
259272

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);
273+
ret = source_get_data(sources[0], codec->mpd.in_buff_size, &src_ptr,
274+
(const void **)&source_buffer_start, &src_bytes);
275+
if (ret)
276+
return ret;
277+
278+
cadence_copy_data_from_buffer(codec->mpd.in_buff, src_ptr,
279+
codec->mpd.in_buff_size, src_bytes,
280+
source_buffer_start);
263281
codec->mpd.avail = codec->mpd.in_buff_size;
264282

265283
comp_dbg(dev, "cadence_codec_process() start");
266284

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

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;
300+
/* Copy the produced samples into the output buffer */
301+
cadence_copy_data_to_buffer(sink_ptr, codec->mpd.produced, sink_bytes,
302+
sink_buffer_start, codec->mpd.out_buff);
274303

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;
304+
sink_commit_buffer(sinks[0], codec->mpd.produced);
279305

280306
comp_dbg(dev, "cadence_codec_process() done");
281307

@@ -307,7 +333,7 @@ static int cadence_codec_reset(struct processing_module *mod)
307333
static const struct module_interface cadence_codec_interface = {
308334
.init = cadence_codec_init,
309335
.prepare = cadence_codec_prepare,
310-
.process_raw_data = cadence_codec_process,
336+
.process = cadence_codec_process,
311337
.set_configuration = cadence_codec_set_configuration,
312338
.reset = cadence_codec_reset,
313339
.free = cadence_codec_free

src/audio/module_adapter/module/cadence_ipc4.c

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,6 @@ 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
{
@@ -509,30 +491,18 @@ static int cadence_codec_process(struct processing_module *mod, struct sof_sourc
509491

510492
void *sink_ptr;
511493
size_t sink_bytes;
512-
uint8_t const *sink_buffer_start;
494+
uint8_t *sink_buffer_start;
513495

514-
ret = sink_get_buffer(sinks[0], codec->mpd.produced, &sink_ptr,
515-
(void **)&sink_buffer_start, &sink_bytes);
496+
ret = sink_get_buffer(sinks[0], codec->mpd.produced, &sink_ptr, &sink_buffer_start,
497+
&sink_bytes);
516498
if (ret) {
517499
comp_err(dev, "cannot get sink buffer");
518500
return ret;
519501
}
520502

521503
/* 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-
}
504+
cadence_copy_data_to_buffer(sink_ptr, codec->mpd.produced, sink_bytes,
505+
sink_buffer_start, codec->mpd.out_buff);
536506

537507
source_release_data(sources[0], codec->mpd.consumed);
538508
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, uint8_t const *buffer_start);
112+
void cadence_copy_data_to_buffer(void *buffer_ptr, size_t bytes_to_copy,
113+
size_t buffer_size, uint8_t *buffer_start,
114+
const void *src);
110115

111116
#endif /* __SOF_AUDIO_CADENCE_CODEC__ */

0 commit comments

Comments
 (0)