From 305f8a2088aa4bcd1a2d8db7d9b652155e0291a6 Mon Sep 17 00:00:00 2001 From: Marcin Szkudlinski Date: Mon, 17 Apr 2023 15:41:00 +0200 Subject: [PATCH] pipeline2.0: introduce sink/source utilities There are many operations on sink/source that may be put into a common library. This is the place for it. Signed-off-by: Marcin Szkudlinski --- src/audio/CMakeLists.txt | 1 + src/audio/sink_source_utils.c | 73 +++++++++++++++++++ src/include/sof/audio/sink_source_utils.h | 25 +++++++ test/cmocka/src/audio/buffer/CMakeLists.txt | 4 + .../cmocka/src/audio/component/CMakeLists.txt | 1 + test/cmocka/src/audio/eq_fir/CMakeLists.txt | 1 + test/cmocka/src/audio/eq_iir/CMakeLists.txt | 1 + test/cmocka/src/audio/mixer/CMakeLists.txt | 1 + test/cmocka/src/audio/mux/CMakeLists.txt | 1 + .../src/audio/pcm_converter/CMakeLists.txt | 1 + test/cmocka/src/audio/pipeline/CMakeLists.txt | 3 + test/cmocka/src/audio/selector/CMakeLists.txt | 1 + test/cmocka/src/audio/volume/CMakeLists.txt | 1 + test/cmocka/src/math/fft/CMakeLists.txt | 1 + zephyr/CMakeLists.txt | 1 + 15 files changed, 116 insertions(+) create mode 100644 src/audio/sink_source_utils.c create mode 100644 src/include/sof/audio/sink_source_utils.h diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 7f5b17339..b43c303aa 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -13,6 +13,7 @@ if((NOT CONFIG_LIBRARY) OR CONFIG_LIBRARY_STATIC) buffer.c source_api_helper.c sink_api_helper.c + sink_source_utils.c channel_map.c ) if(CONFIG_COMP_BLOB) diff --git a/src/audio/sink_source_utils.c b/src/audio/sink_source_utils.c new file mode 100644 index 000000000..671c1752d --- /dev/null +++ b/src/audio/sink_source_utils.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2023 Intel Corporation. All rights reserved. +// + +#include +#include +#include +#include +#include +#include +#include +#include + +int source_to_sink_copy(struct sof_source __sparse_cache *source, + struct sof_sink __sparse_cache *sink, bool free, size_t size) +{ + uint8_t *src_ptr; + uint8_t *src_begin; + uint8_t *src_end; + uint8_t *dst_ptr; + uint8_t *dst_begin; + uint8_t *dst_end; + size_t src_size, dst_size; + int ret; + + if (!size) + return 0; + if (size > source_get_data_available(source)) + return -EFBIG; + if (size > sink_get_free_size(sink)) + return -ENOSPC; + + ret = source_get_data(source, size, + (void **)&src_ptr, + (void **)&src_begin, + &src_size); + if (ret) + return ret; + + ret = sink_get_buffer(sink, size, + (void **)&dst_ptr, + (void **)&dst_begin, + &dst_size); + if (ret) { + source_release_data(source, 0); + return ret; + } + + src_end = src_begin + src_size; + dst_end = dst_begin + dst_size; + while (size) { + uint32_t src_to_buf_overlap = (uintptr_t)src_end - (uintptr_t)src_ptr; + uint32_t dst_to_buf_overlap = (uintptr_t)dst_end - (uintptr_t)dst_ptr; + uint32_t to_copy = MIN(src_to_buf_overlap, dst_to_buf_overlap); + + to_copy = MIN(to_copy, size); + ret = memcpy_s(dst_ptr, dst_to_buf_overlap, src_ptr, to_copy); + assert(!ret); + + size -= to_copy; + src_ptr += to_copy; + dst_ptr += to_copy; + if (to_copy == src_to_buf_overlap) + src_ptr = src_begin; + if (to_copy == dst_to_buf_overlap) + dst_ptr = dst_begin; + } + + source_release_data(source, free ? INT_MAX : 0); + sink_commit_buffer(sink, INT_MAX); + return 0; +} diff --git a/src/include/sof/audio/sink_source_utils.h b/src/include/sof/audio/sink_source_utils.h new file mode 100644 index 000000000..8f550b1d2 --- /dev/null +++ b/src/include/sof/audio/sink_source_utils.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2023 Intel Corporation. All rights reserved. + * + */ + +#ifndef SINK_SOURCE_UTILS_H +#define SINK_SOURCE_UTILS_H + +#include +#include + +/** + * copy bytes from source to sink + * + * @param source the data source + * @param sink the data target + * @param free if true, data from source will be freed + * if false, data will remain in the source + * @param size number of bytes to be copied + */ +int source_to_sink_copy(struct sof_source __sparse_cache *source, + struct sof_sink __sparse_cache *sink, bool free, size_t size); + +#endif /* SINK_SOURCE_UTILS_H */ diff --git a/test/cmocka/src/audio/buffer/CMakeLists.txt b/test/cmocka/src/audio/buffer/CMakeLists.txt index 0d14c5bb8..1683577c3 100644 --- a/test/cmocka/src/audio/buffer/CMakeLists.txt +++ b/test/cmocka/src/audio/buffer/CMakeLists.txt @@ -6,6 +6,7 @@ cmocka_test(buffer_copy ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c @@ -26,6 +27,7 @@ cmocka_test(buffer_new ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c @@ -45,6 +47,7 @@ cmocka_test(buffer_wrap ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c @@ -64,6 +67,7 @@ cmocka_test(buffer_write ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c diff --git a/test/cmocka/src/audio/component/CMakeLists.txt b/test/cmocka/src/audio/component/CMakeLists.txt index 72385c0a0..7534a0270 100644 --- a/test/cmocka/src/audio/component/CMakeLists.txt +++ b/test/cmocka/src/audio/component/CMakeLists.txt @@ -12,6 +12,7 @@ cmocka_test(comp_set_state ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c diff --git a/test/cmocka/src/audio/eq_fir/CMakeLists.txt b/test/cmocka/src/audio/eq_fir/CMakeLists.txt index e1dd9f7fd..94c3ff0f4 100644 --- a/test/cmocka/src/audio/eq_fir/CMakeLists.txt +++ b/test/cmocka/src/audio/eq_fir/CMakeLists.txt @@ -25,6 +25,7 @@ add_library(audio_for_eq_fir STATIC ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/audio/component.c ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c diff --git a/test/cmocka/src/audio/eq_iir/CMakeLists.txt b/test/cmocka/src/audio/eq_iir/CMakeLists.txt index 6b98a07c8..5d28dc64d 100644 --- a/test/cmocka/src/audio/eq_iir/CMakeLists.txt +++ b/test/cmocka/src/audio/eq_iir/CMakeLists.txt @@ -25,6 +25,7 @@ add_library(audio_for_eq_iir STATIC ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/audio/component.c ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c diff --git a/test/cmocka/src/audio/mixer/CMakeLists.txt b/test/cmocka/src/audio/mixer/CMakeLists.txt index eaaaad80a..41a568e47 100644 --- a/test/cmocka/src/audio/mixer/CMakeLists.txt +++ b/test/cmocka/src/audio/mixer/CMakeLists.txt @@ -15,6 +15,7 @@ cmocka_test(mixer ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/audio/component.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c diff --git a/test/cmocka/src/audio/mux/CMakeLists.txt b/test/cmocka/src/audio/mux/CMakeLists.txt index ed3c42eaa..535c042a0 100644 --- a/test/cmocka/src/audio/mux/CMakeLists.txt +++ b/test/cmocka/src/audio/mux/CMakeLists.txt @@ -16,6 +16,7 @@ add_library( ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/math/numbers.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c diff --git a/test/cmocka/src/audio/pcm_converter/CMakeLists.txt b/test/cmocka/src/audio/pcm_converter/CMakeLists.txt index d88019810..aad4e3b47 100644 --- a/test/cmocka/src/audio/pcm_converter/CMakeLists.txt +++ b/test/cmocka/src/audio/pcm_converter/CMakeLists.txt @@ -9,6 +9,7 @@ if(CONFIG_FORMAT_FLOAT) ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/audio/component.c ${PROJECT_SOURCE_DIR}/src/audio/data_blob.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c diff --git a/test/cmocka/src/audio/pipeline/CMakeLists.txt b/test/cmocka/src/audio/pipeline/CMakeLists.txt index 6819514f6..9b3c17824 100644 --- a/test/cmocka/src/audio/pipeline/CMakeLists.txt +++ b/test/cmocka/src/audio/pipeline/CMakeLists.txt @@ -27,6 +27,7 @@ cmocka_test(pipeline_new ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c @@ -47,6 +48,7 @@ cmocka_test(pipeline_connect_upstream ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c @@ -67,6 +69,7 @@ cmocka_test(pipeline_free ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c ${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c diff --git a/test/cmocka/src/audio/selector/CMakeLists.txt b/test/cmocka/src/audio/selector/CMakeLists.txt index a955f147a..a7f83fc65 100644 --- a/test/cmocka/src/audio/selector/CMakeLists.txt +++ b/test/cmocka/src/audio/selector/CMakeLists.txt @@ -18,6 +18,7 @@ add_library(audio_for_selector STATIC ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c diff --git a/test/cmocka/src/audio/volume/CMakeLists.txt b/test/cmocka/src/audio/volume/CMakeLists.txt index 7d50d0c70..150d10b25 100644 --- a/test/cmocka/src/audio/volume/CMakeLists.txt +++ b/test/cmocka/src/audio/volume/CMakeLists.txt @@ -22,6 +22,7 @@ add_library(audio_for_volume STATIC ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c diff --git a/test/cmocka/src/math/fft/CMakeLists.txt b/test/cmocka/src/math/fft/CMakeLists.txt index 11c5edcd0..08ebbcdaf 100644 --- a/test/cmocka/src/math/fft/CMakeLists.txt +++ b/test/cmocka/src/math/fft/CMakeLists.txt @@ -10,6 +10,7 @@ cmocka_test(fft ${PROJECT_SOURCE_DIR}/src/audio/buffer.c ${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c ${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c + ${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c ${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c ${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index de0046c6d..7da2c3a1c 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -396,6 +396,7 @@ zephyr_library_sources( ${SOF_AUDIO_PATH}/buffer.c ${SOF_AUDIO_PATH}/source_api_helper.c ${SOF_AUDIO_PATH}/sink_api_helper.c + ${SOF_AUDIO_PATH}/sink_source_utils.c ${SOF_AUDIO_PATH}/component.c ${SOF_AUDIO_PATH}/pipeline/pipeline-graph.c ${SOF_AUDIO_PATH}/pipeline/pipeline-params.c