diff --git a/src/arch/host/CMakeLists.txt b/src/arch/host/CMakeLists.txt index 4bfd37396cfb..839f995917f4 100644 --- a/src/arch/host/CMakeLists.txt +++ b/src/arch/host/CMakeLists.txt @@ -18,6 +18,7 @@ set(CONFIG_COMP_MIXER 1) set(CONFIG_COMP_MUX 1) set(CONFIG_COMP_SWITCH 1) set(CONFIG_COMP_DAI 1) +set(CONFIG_COMP_SEL 1) configure_file ( "${PROJECT_SOURCE_DIR}/src/arch/host/config.h.in" diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 4fcd8717b7bb..9dca5e9bb6e4 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -61,6 +61,12 @@ if(NOT BUILD_HOST) dai.c ) endif() + if(CONFIG_COMP_SEL) + add_local_sources(sof + selector.c + selector_generic.c + ) + endif() return() endif() diff --git a/src/audio/Kconfig b/src/audio/Kconfig index 9e485994b83d..607f8ddf22d5 100644 --- a/src/audio/Kconfig +++ b/src/audio/Kconfig @@ -60,4 +60,10 @@ config COMP_SWITCH help Select for KPB component +config COMP_SEL + bool "Channel selector component" + default y + help + Select for SEL component + endmenu diff --git a/src/audio/selector.c b/src/audio/selector.c new file mode 100644 index 000000000000..120244302177 --- /dev/null +++ b/src/audio/selector.c @@ -0,0 +1,474 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Lech Betlej + */ + +/** + * \file audio/selector.c + * \brief Audio channel selection component. In case 1 output channel is + * \brief selected in topology the component provides the selected channel on + * \brief ouput. In case 2 or 4 channels are selected on output the component + * \brief works in a passthrough mode. + * \authors Lech Betlej + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "selector.h" +#include + + +/** + * \brief Validates channel count and index and sets channel count. + * \details If input data is not supported trace error is displayed and + * \details input data is modified to safe values. + * \param[in,out] cd Selector component private data. + * \param[in] in_channels Number of input channels to the module. + * \param[in] out_channels Number of output channels to the module. + * \param[in] ch_idx Index of a channel to be provided on output. + * \return Status. + */ +static int sel_set_channel_values(struct comp_data *cd, uint32_t in_channels, + uint32_t out_channels, uint32_t ch_idx) +{ + /* verify input channels */ + if (in_channels != SEL_SOURCE_2CH && in_channels != SEL_SOURCE_4CH) { + trace_selector_error("sel_set_channel_values() error: " + "in_channels = %u", in_channels); + return -EINVAL; + } + + /* verify output channels */ + if (out_channels != SEL_SINK_1CH && out_channels != SEL_SINK_2CH && + out_channels != SEL_SINK_4CH) { + trace_selector_error("sel_set_channel_values() error: " + "out_channels = %u", out_channels); + return -EINVAL; + } + + /* verify proper channels for passthrough mode */ + if (out_channels != SEL_SINK_1CH && in_channels != out_channels) { + trace_selector_error("sel_set_channel_values() error: " + "in_channels = %u, out_channels = %u", + in_channels, out_channels); + return -EINVAL; + } + + if (ch_idx > (SEL_SOURCE_4CH - 1)) { + trace_selector_error("sel_set_channel_values() error: " + "ch_idx = %u", in_channels); + return -EINVAL; + } + + cd->in_channels_count = in_channels; + cd->out_channels_count = out_channels; + cd->sel_channel = ch_idx; + + return 0; +} + +/** + * \brief Creates selector component. + * \param[in,out] data Selector base component device. + * \return Pointer to selector base component device. + */ +static struct comp_dev *selector_new(struct sof_ipc_comp *comp) +{ + struct comp_dev *dev; + struct sof_ipc_comp_selector *sel; + struct sof_ipc_comp_selector *ipc_sel = + (struct sof_ipc_comp_selector *)comp; + struct comp_data *cd; + int ret; + + trace_selector("selector_new()"); + + if (IPC_IS_SIZE_INVALID(ipc_sel->config)) { + IPC_SIZE_ERROR_TRACE(TRACE_CLASS_SELECTOR, ipc_sel->config); + return NULL; + } + + dev = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, + COMP_SIZE(struct sof_ipc_comp_selector)); + if (!dev) + return NULL; + + sel = (struct sof_ipc_comp_selector *)&dev->comp; + memcpy(sel, ipc_sel, sizeof(struct sof_ipc_comp_selector)); + + cd = rzalloc(RZONE_RUNTIME, SOF_MEM_CAPS_RAM, sizeof(*cd)); + if (!cd) { + rfree(dev); + return NULL; + } + + comp_set_drvdata(dev, cd); + + /* verification of initial parameters */ + ret = sel_set_channel_values(cd, ipc_sel->input_channels_count, + ipc_sel->output_channels_count, + ipc_sel->selected_channel); + if (ret < 0) { + rfree(cd); + rfree(dev); + return NULL; + } + + dev->state = COMP_STATE_READY; + return dev; +} + +/** + * \brief Frees selector component. + * \param[in,out] dev Selector base component device. + */ +static void selector_free(struct comp_dev *dev) +{ + struct comp_data *cd = comp_get_drvdata(dev); + + trace_selector("selector_free()"); + + rfree(cd); + rfree(dev); +} + +/** + * \brief Sets selector component audio stream parameters. + * \param[in,out] dev Selector base component device. + * \return Error code. + * + * All done in prepare since we need to know source and sink component params. + */ +static int selector_params(struct comp_dev *dev) +{ + struct comp_data *cd = comp_get_drvdata(dev); + + trace_selector("selector_params()"); + + /* rewrite channels number for other components */ + if (dev->params.direction == SOF_IPC_STREAM_PLAYBACK) + dev->params.channels = cd->out_channels_count; + else + dev->params.channels = cd->in_channels_count; + + return 0; +} + + +/** + * \brief Sets selector control command. + * \param[in,out] dev Selector base component device. + * \param[in,out] cdata Control command data. + * \return Error code. + */ +static int selector_ctrl_set_cmd(struct comp_dev *dev, + struct sof_ipc_ctrl_data *cdata) +{ + /* TODO add implementation aligned with SW assumptions */ + trace_selector("selector_ctrl_set_cmd(), cdata->cmd = %u", cdata->cmd); + + return 0; +} + +/** + * \brief Gets selector control command. + * \param[in,out] dev Selector base component device. + * \param[in,out] cdata Selector command data. + * \param[in] size Command data size. + * \return Error code. + */ +static int selector_ctrl_get_cmd(struct comp_dev *dev, + struct sof_ipc_ctrl_data *cdata, int size) +{ + /* TODO add implementation aligned with SW assumptions */ + cdata->cmd = SOF_CTRL_CMD_ENUM; + trace_selector("selector_ctrl_get_cmd(), cdata->cmd = %u", + cdata->index); + + return 0; +} + +/** + * \brief Used to pass standard and bespoke commands (with data) to component. + * \param[in,out] dev Selector base component device. + * \param[in] cmd Command type. + * \param[in,out] data Control command data. + * \param[in] max_data_size Command max data size. + * \return Error code. + */ +static int selector_cmd(struct comp_dev *dev, int cmd, void *data, + int max_data_size) +{ + struct sof_ipc_ctrl_data *cdata = data; + + trace_selector("selector_cmd()"); + + switch (cmd) { + case COMP_CMD_SET_VALUE: + return selector_ctrl_set_cmd(dev, cdata); + case COMP_CMD_GET_VALUE: + return selector_ctrl_get_cmd(dev, cdata, max_data_size); + default: + return -EINVAL; + } +} + +/** + * \brief Sets component state. + * \param[in,out] dev Selector base component device. + * \param[in] cmd Command type. + * \return Error code. + */ +static int selector_trigger(struct comp_dev *dev, int cmd) +{ + trace_selector("selector_trigger()"); + + return comp_set_state(dev, cmd); +} + + +/** + * \brief Copies and processes stream data. + * \param[in,out] dev Selector base component device. + * \return Error code. + */ +static int selector_copy(struct comp_dev *dev) +{ + struct comp_data *cd = comp_get_drvdata(dev); + struct comp_buffer *sink; + struct comp_buffer *source; + uint32_t frames; + uint32_t source_bytes; + uint32_t sink_bytes; + + tracev_selector("selector_copy()"); + + /* selector component will have 1 source and 1 sink buffer */ + source = list_first_item(&dev->bsource_list, struct comp_buffer, + sink_list); + sink = list_first_item(&dev->bsink_list, struct comp_buffer, + source_list); + + /* check for underrun */ + if (source->avail == 0) { + trace_selector_error("selector_copy() error: " + "source component buffer has not enough " + "data available"); + comp_underrun(dev, source, 0, 0); + return -EIO; + } + + /* check for overrun */ + if (sink->free == 0) { + trace_selector_error("selector_copy() error: " + "sink component buffer has not enough " + "free bytes for copy"); + comp_overrun(dev, sink, 0, 0); + return -EIO; + } + + frames = comp_avail_frames(source, sink); + source_bytes = frames * comp_frame_bytes(source->source); + sink_bytes = frames * comp_frame_bytes(sink->sink); + + tracev_selector("selector_copy(), source_bytes = 0x%x, sink_bytes = " + "0x%x", source_bytes, sink_bytes); + + /* copy selected channels from in to out */ + cd->sel_func(dev, sink, source, frames); + + /* calculate new free and available */ + comp_update_buffer_produce(sink, sink_bytes); + comp_update_buffer_consume(source, source_bytes); + + return 0; +} + +/** + * \brief Prepares selector component for processing. + * \param[in,out] dev Selector base component device. + * \return Error code. + */ +static int selector_prepare(struct comp_dev *dev) +{ + struct comp_data *cd = comp_get_drvdata(dev); + struct comp_buffer *sinkb; + struct comp_buffer *sourceb; + struct sof_ipc_comp_config *config = COMP_GET_CONFIG(dev); + int ret; + + trace_selector("selector_prepare()"); + + ret = comp_set_state(dev, COMP_TRIGGER_PREPARE); + if (ret) + return ret; + + /* selector component will have 1 source and 1 sink buffer */ + sourceb = list_first_item(&dev->bsource_list, struct comp_buffer, + sink_list); + sinkb = list_first_item(&dev->bsink_list, struct comp_buffer, + source_list); + + /* get source data format and period bytes */ + comp_set_period_bytes(sourceb->source, dev->frames, &cd->source_format, + &cd->source_period_bytes); + + /* get sink data format and period bytes */ + comp_set_period_bytes(sinkb->sink, dev->frames, &cd->sink_format, + &cd->sink_period_bytes); + + /* There is an assumption that sink component will report out + * proper number of channels [1] for selector to actually + * reduce channel count between source and sink + */ + trace_selector("selector_prepare(): source->params.channels = %u", + sourceb->sink->params.channels); + trace_selector("selector_prepare(): sink->params.channels = %u", + sinkb->sink->params.channels); + + /* set downstream buffer size */ + ret = buffer_set_size(sinkb, cd->sink_period_bytes * + config->periods_sink); + if (ret < 0) { + trace_selector_error("selector_prepare() error: " + "buffer_set_size() failed"); + goto err; + } + + /* validate */ + if (cd->sink_period_bytes == 0) { + trace_selector_error("selector_prepare() error: " + "cd->sink_period_bytes = 0, dev->frames =" + " %u, sinkb->sink->frame_bytes = %u", + dev->frames, sinkb->sink->frame_bytes); + ret = -EINVAL; + goto err; + } + + if (cd->source_period_bytes == 0) { + trace_selector_error("selector_prepare() error: " + "cd->source_period_bytes = 0, " + "dev->frames = %u, " + "sourceb->source->frame_bytes = %u", + dev->frames, + sourceb->source->frame_bytes); + ret = -EINVAL; + goto err; + } + + cd->sel_func = sel_get_processing_function(dev); + if (!cd->sel_func) { + trace_selector_error("selector_prepare() error: " + "invalid cd->sel_func, " + "cd->source_format = %u, " + "cd->sink_format = %u, " + "cd->out_channels_count = %u", + cd->source_format, cd->sink_format, + cd->out_channels_count); + ret = -EINVAL; + goto err; + } + + return 0; + +err: + comp_set_state(dev, COMP_TRIGGER_RESET); + return ret; +} + +/** + * \brief Resets selector component. + * \param[in,out] dev Selector base component device. + * \return Error code. + */ +static int selector_reset(struct comp_dev *dev) +{ + trace_selector("selector_reset()"); + + return comp_set_state(dev, COMP_TRIGGER_RESET); +} + +/** + * \brief Executes cache operation on selector component. + * \param[in,out] dev Selector base component device. + * \param[in] cmd Cache command. + */ +static void selector_cache(struct comp_dev *dev, int cmd) +{ + struct comp_data *cd; + + switch (cmd) { + case CACHE_WRITEBACK_INV: + trace_selector("selector_cache(), CACHE_WRITEBACK_INV"); + + cd = comp_get_drvdata(dev); + + dcache_writeback_invalidate_region(cd, sizeof(*cd)); + dcache_writeback_invalidate_region(dev, sizeof(*dev)); + break; + + case CACHE_INVALIDATE: + trace_selector("selector_cache(), CACHE_INVALIDATE"); + + dcache_invalidate_region(dev, sizeof(*dev)); + + cd = comp_get_drvdata(dev); + dcache_invalidate_region(cd, sizeof(*cd)); + break; + } +} + +/** \brief Selector component definition. */ +struct comp_driver comp_selector = { + .type = SOF_COMP_SELECTOR, + .ops = { + .new = selector_new, + .free = selector_free, + .params = selector_params, + .cmd = selector_cmd, + .trigger = selector_trigger, + .copy = selector_copy, + .prepare = selector_prepare, + .reset = selector_reset, + .cache = selector_cache, + }, +}; + +/** \brief Initializes selector component. */ +void sys_comp_selector_init(void) +{ + comp_register(&comp_selector); +} diff --git a/src/audio/selector.h b/src/audio/selector.h new file mode 100644 index 000000000000..9519526afcdd --- /dev/null +++ b/src/audio/selector.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Lech Betlej + */ + +/** + * \file audio/selector.h + * \brief Channel selector component header file + * \authors Lech Betlej + */ + +#ifndef SELECTOR_H +#define SELECTOR_H + +#include +#include +#include +#include + +/** \brief Selector trace function. */ +#define trace_selector(__e, ...) \ + trace_event(TRACE_CLASS_SELECTOR, __e, ##__VA_ARGS__) + +/** \brief Selector trace verbose function. */ +#define tracev_selector(__e, ...) \ + tracev_event(TRACE_CLASS_SELECTOR, __e, ##__VA_ARGS__) + +/** \brief Selector trace error function. */ +#define trace_selector_error(__e, ...) \ + trace_error(TRACE_CLASS_SELECTOR, __e, ##__VA_ARGS__) + +/** \brief Supported channel count on input. */ +#define SEL_SOURCE_2CH 2 +#define SEL_SOURCE_4CH 4 + +/** \brief Supported channel count on output. */ +#define SEL_SINK_1CH 1 +#define SEL_SINK_2CH 2 +#define SEL_SINK_4CH 4 + +/** \brief Selector component private data. */ +struct comp_data { + uint32_t source_period_bytes; /**< source number of period bytes */ + uint32_t sink_period_bytes; /**< sink number of period bytes */ + enum sof_ipc_frame source_format; /**< source frame format */ + enum sof_ipc_frame sink_format; /**< sink frame format */ + + /* selector supports 1 input and 1 output */ + uint32_t in_channels_count; /**< accepted values 2 or 4 */ + uint32_t out_channels_count; /**< accepted values 1 or 2 or 4 */ + + /* note: if 2 or 4 output channels selected the component works in + * a passthrough mode + */ + uint32_t sel_channel; /**< 0..3 */ + + /**< channel selector processing function */ + void (*sel_func)(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source, uint32_t frames); +}; + +/** \brief Selector processing functions map. */ +struct comp_func_map { + uint16_t source; /**< source frame format */ + uint32_t out_channels; /**< number of output stream channels */ + /**< selector processing function */ + void (*sel_func)(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source, uint32_t frames); +}; + +/** \brief Map of formats with dedicated processing functions. */ +extern const struct comp_func_map func_map[]; + +typedef void (*sel_func)(struct comp_dev *, struct comp_buffer *, + struct comp_buffer *, uint32_t); + +/** + * \brief Retrieves selector processing function. + * \param[in,out] dev Selector base component device. + */ +sel_func sel_get_processing_function(struct comp_dev *dev); + +#endif /* SELECTOR_H */ diff --git a/src/audio/selector_generic.c b/src/audio/selector_generic.c new file mode 100644 index 000000000000..8322b49209fb --- /dev/null +++ b/src/audio/selector_generic.c @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Lech Betlej + */ + +/** + * \file audio/selector_generic.c + * \brief Audio channel selector / extractor - generic processing functions + * \authors Lech Betlej + */ + +#include "selector.h" + +/** + * \brief Channel selection for 16 bit, 1 channel data format. + * \param[in,out] dev Selector base component device. + * \param[in,out] sink Destination buffer. + * \param[in,out] source Source buffer. + * \param[in] frames Number of frames to process. + */ +static void sel_s16le_1ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source, uint32_t frames) +{ + struct comp_data *cd = comp_get_drvdata(dev); + int16_t *src; + int16_t *dest; + uint32_t i; + uint32_t j = 0; + uint32_t nch = cd->in_channels_count; + + for (i = cd->sel_channel; i < frames * nch; i += nch) { + src = buffer_read_frag_s16(source, i); + dest = buffer_write_frag_s16(sink, j++); + *dest = *src; + } +} + +/** + * \brief Channel selection for 32 bit, 1 channel data format. + * \param[in,out] dev Selector base component device. + * \param[in,out] sink Destination buffer. + * \param[in,out] source Source buffer. + * \param[in] frames Number of frames to process. + */ +static void sel_s32le_1ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source, uint32_t frames) +{ + struct comp_data *cd = comp_get_drvdata(dev); + int32_t *src; + int32_t *dest; + uint32_t i; + uint32_t j = 0; + uint32_t nch = cd->in_channels_count; + + for (i = cd->sel_channel; i < frames * nch; i += nch) { + src = buffer_read_frag_s32(source, i); + dest = buffer_write_frag_s32(sink, j++); + *dest = *src; + } +} + +/** + * \brief Channel selection for 16 bit, at least 2 channels data format. + * \param[in,out] dev Selector base component device. + * \param[in,out] sink Destination buffer. + * \param[in,out] source Source buffer. + * \param[in] frames Number of frames to process. + */ +static void sel_s16le_nch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source, uint32_t frames) +{ + struct comp_data *cd = comp_get_drvdata(dev); + int16_t *src; + int16_t *dest; + uint32_t i; + uint32_t j = 0; + uint32_t channel; + + for (i = 0; i < frames; i++) { + for (channel = 0; channel < cd->in_channels_count; channel++) { + src = buffer_read_frag_s16(source, j); + dest = buffer_write_frag_s16(sink, j); + *dest = *src; + j++; + } + } +} + +/** + * \brief Channel selection for 32 bit, at least 2 channels data format. + * \param[in,out] dev Selector base component device. + * \param[in,out] sink Destination buffer. + * \param[in,out] source Source buffer. + * \param[in] frames Number of frames to process. + */ +static void sel_s32le_nch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source, uint32_t frames) +{ + struct comp_data *cd = comp_get_drvdata(dev); + int32_t *src; + int32_t *dest; + uint32_t i; + uint32_t j = 0; + uint32_t channel; + + for (i = 0; i < frames; i++) { + for (channel = 0; channel < cd->in_channels_count; channel++) { + src = buffer_read_frag_s32(source, j); + dest = buffer_write_frag_s32(sink, j); + *dest = *src; + j++; + } + } +} + +const struct comp_func_map func_table[] = { + {SOF_IPC_FRAME_S16_LE, 1, sel_s16le_1ch}, + {SOF_IPC_FRAME_S24_4LE, 1, sel_s32le_1ch}, + {SOF_IPC_FRAME_S32_LE, 1, sel_s32le_1ch}, + {SOF_IPC_FRAME_S16_LE, 2, sel_s16le_nch}, + {SOF_IPC_FRAME_S24_4LE, 2, sel_s32le_nch}, + {SOF_IPC_FRAME_S32_LE, 2, sel_s32le_nch}, + {SOF_IPC_FRAME_S16_LE, 4, sel_s16le_nch}, + {SOF_IPC_FRAME_S24_4LE, 4, sel_s32le_nch}, + {SOF_IPC_FRAME_S32_LE, 4, sel_s32le_nch}, +}; + +sel_func sel_get_processing_function(struct comp_dev *dev) +{ + struct comp_data *cd = comp_get_drvdata(dev); + int i; + + /* map the channel selection function for source and sink buffers */ + for (i = 0; i < ARRAY_SIZE(func_table); i++) { + if (cd->source_format != func_table[i].source) + continue; + if (cd->out_channels_count != func_table[i].out_channels) + continue; + + /* TODO: add additional criteria as needed */ + return func_table[i].sel_func; + } + + return NULL; +} diff --git a/src/include/sof/audio/component.h b/src/include/sof/audio/component.h index b7c92122d320..3d09120d9f32 100644 --- a/src/include/sof/audio/component.h +++ b/src/include/sof/audio/component.h @@ -546,6 +546,12 @@ void sys_comp_kpb_init(void); #else static inline void sys_comp_kpb_init(void) {} #endif +#ifdef CONFIG_COMP_SEL +void sys_comp_selector_init(void); +#else +static inline void sys_comp_selector_init(void) {} +#endif + /** @}*/ /** \name Helpers. diff --git a/src/include/sof/trace.h b/src/include/sof/trace.h index fa00aa98b14e..c07c7eb7d61b 100644 --- a/src/include/sof/trace.h +++ b/src/include/sof/trace.h @@ -111,6 +111,7 @@ #define TRACE_CLASS_CLK (26 << 24) #define TRACE_CLASS_SCHEDULE (27 << 24) #define TRACE_CLASS_KPB (28 << 24) +#define TRACE_CLASS_SELECTOR (29 << 24) /* move to config.h */ #define TRACE 1 diff --git a/src/include/uapi/ipc/topology.h b/src/include/uapi/ipc/topology.h index 0e26b14cb4ab..b28aea88e060 100644 --- a/src/include/uapi/ipc/topology.h +++ b/src/include/uapi/ipc/topology.h @@ -65,6 +65,7 @@ enum sof_comp_type { SOF_COMP_FILEREAD, /**< host test based file IO */ SOF_COMP_FILEWRITE, /**< host test based file IO */ SOF_COMP_KPB, /* A key phrase buffer component */ + SOF_COMP_SELECTOR, }; /* XRUN action for component */ @@ -163,6 +164,21 @@ struct sof_ipc_comp_volume { uint32_t initial_ramp; /**< ramp space in ms */ } __attribute__((packed)); +/* generic selector component */ +struct sof_ipc_comp_selector { + struct sof_ipc_comp comp; + struct sof_ipc_comp_config config; + + /* selector supports 1 input and 1 output */ + uint32_t input_channels_count; /**< accepted values 2 or 4 */ + uint32_t output_channels_count; /**< accepted values 1 or 2 or 4 */ + + /* note: if 2 or 4 output channels selected, the component works + * in a bypass mode + */ + uint32_t selected_channel; /**< 0..3 */ +} __attribute__((packed)); + /* generic SRC component */ struct sof_ipc_comp_src { struct sof_ipc_comp comp; diff --git a/src/tasks/audio.c b/src/tasks/audio.c index 259c34bdfdfa..c963df07d891 100644 --- a/src/tasks/audio.c +++ b/src/tasks/audio.c @@ -64,6 +64,7 @@ int do_task_master_core(struct sof *sof) sys_comp_tone_init(); sys_comp_eq_iir_init(); sys_comp_eq_fir_init(); + sys_comp_selector_init(); #if STATIC_PIPE /* init static pipeline */ diff --git a/test/cmocka/src/audio/CMakeLists.txt b/test/cmocka/src/audio/CMakeLists.txt index 179a3332fd56..e5f89b290023 100644 --- a/test/cmocka/src/audio/CMakeLists.txt +++ b/test/cmocka/src/audio/CMakeLists.txt @@ -9,4 +9,7 @@ if(CONFIG_COMP_VOLUME) endif() if(CONFIG_COMP_VOLUME) add_subdirectory(kpb) -endif() \ No newline at end of file +endif() +if(CONFIG_COMP_SEL) + add_subdirectory(selector) +endif() diff --git a/test/cmocka/src/audio/selector/CMakeLists.txt b/test/cmocka/src/audio/selector/CMakeLists.txt new file mode 100644 index 000000000000..088d9e7cf3b8 --- /dev/null +++ b/test/cmocka/src/audio/selector/CMakeLists.txt @@ -0,0 +1,16 @@ +cmocka_test(selector_test + selector_test.c +) + +target_include_directories(selector_test PRIVATE ${PROJECT_SOURCE_DIR}/src/audio) + +# make small version of libaudio so we don't have to care +# about unused missing references +add_library(audio_for_selector STATIC + ${PROJECT_SOURCE_DIR}/src/audio/selector.c + ${PROJECT_SOURCE_DIR}/src/audio/selector_generic.c +) + +target_link_libraries(audio_for_selector PRIVATE sof_options) + +target_link_libraries(selector_test PRIVATE audio_for_selector) diff --git a/test/cmocka/src/audio/selector/selector_test.c b/test/cmocka/src/audio/selector/selector_test.c new file mode 100644 index 000000000000..180dff6530cb --- /dev/null +++ b/test/cmocka/src/audio/selector/selector_test.c @@ -0,0 +1,336 @@ +/* + * Copyright (c) 2019, Intel Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the Intel Corporation nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * Author: Lech Betlej + */ + +#include +#include +#include +#include +#include +#include "selector.h" + + +struct sel_test_state { + struct comp_dev *dev; + struct comp_buffer *sink; + struct comp_buffer *source; + void (*verify)(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source); +}; + +struct sel_test_parameters { + uint32_t in_channels; + uint32_t out_channels; + uint32_t sel_channel; + uint32_t frames; + uint32_t buffer_size_ms; + uint32_t source_format; + uint32_t sink_format; + void (*verify)(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source); +}; + +static int setup(void **state) +{ + struct sel_test_parameters *parameters = *state; + struct sel_test_state *sel_state; + struct comp_data *cd; + uint32_t size = 0; + + /* allocate new state */ + sel_state = test_malloc(sizeof(*sel_state)); + + /* allocate and set new device */ + sel_state->dev = test_malloc(COMP_SIZE(struct sof_ipc_comp_volume)); + sel_state->dev->params.channels = parameters->in_channels; + sel_state->dev->frames = parameters->frames; + + /* allocate and set new data */ + cd = test_malloc(sizeof(*cd)); + comp_set_drvdata(sel_state->dev, cd); + cd->source_format = parameters->source_format; + cd->sink_format = parameters->sink_format; + + /* prepare paramters that will be used by sel_get_processing_function */ + cd->in_channels_count = parameters->in_channels; + cd->out_channels_count = parameters->out_channels; + cd->sel_channel = parameters->sel_channel; + + cd->sel_func = sel_get_processing_function(sel_state->dev); + + /* allocate new sink buffer */ + sel_state->sink = test_malloc(sizeof(*sel_state->sink)); + sel_state->dev->params.frame_fmt = parameters->sink_format; + size = parameters->frames * comp_frame_bytes(sel_state->dev); + if(cd->out_channels_count == SEL_SINK_1CH){ + size = size / sel_state->dev->params.channels; + } + sel_state->sink->w_ptr = test_calloc(parameters->buffer_size_ms, + size); + sel_state->sink->size = parameters->buffer_size_ms * size; + + /* allocate new source buffer */ + sel_state->source = test_malloc(sizeof(*sel_state->source)); + sel_state->dev->params.frame_fmt = parameters->source_format; + size = parameters->frames * comp_frame_bytes(sel_state->dev); + sel_state->source->r_ptr = test_calloc(parameters->buffer_size_ms, + size); + sel_state->source->size = parameters->buffer_size_ms * size; + + /* assigns verification function */ + sel_state->verify = parameters->verify; + + /* assign test state */ + *state = sel_state; + + return 0; +} + +static int teardown(void **state) +{ + struct sel_test_state *sel_state = *state; + struct comp_data *cd = comp_get_drvdata(sel_state->dev); + + /* free everything */ + test_free(cd); + test_free(sel_state->dev); + test_free(sel_state->sink->w_ptr); + test_free(sel_state->sink); + test_free(sel_state->source->r_ptr); + test_free(sel_state->source); + test_free(sel_state); + + return 0; +} + +static void fill_source_s16(struct sel_test_state *sel_state) +{ + int16_t *src = (int16_t *)sel_state->source->r_ptr; + int i; + + for (i = 0; i < sel_state->source->size / sizeof(int16_t); i++) + src[i] = i; + +} + +static void fill_source_s32(struct sel_test_state *sel_state) +{ + int32_t *src = (int32_t *)sel_state->source->r_ptr; + int i; + + for (i = 0; i < sel_state->source->size / sizeof(int32_t); i++) + src[i] = i << 16; +} + +static void verify_s16le_Xch_to_1ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint16_t *src = (uint16_t *)source->r_ptr; + const uint16_t *dst = (uint16_t *)sink->w_ptr; + uint32_t in_channels = cd->in_channels_count; + uint32_t channel; + uint32_t i; + uint32_t j = 0; + uint16_t source_in; + uint16_t destination; + + for (i = 0; i < source->size / sizeof(uint16_t); i += in_channels) { + for (channel = 0; channel < in_channels; channel++) { + if (channel == cd->sel_channel) { + source_in = src[i + cd->sel_channel]; + destination = dst[j++]; + assert_int_equal(source_in, destination); + } + } + } +} + +static void verify_s32le_Xch_to_1ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint32_t *src = (uint32_t *)source->r_ptr; + const uint32_t *dst = (uint32_t *)sink->w_ptr; + uint32_t in_channels = cd->in_channels_count; + uint32_t channel; + uint32_t i; + uint32_t j = 0; + uint16_t source_in; + uint16_t destination; + + for (i = 0; i < source->size / sizeof(uint32_t); i += in_channels) { + for (channel = 0; channel < in_channels; channel++) { + if (channel == cd->sel_channel) { + source_in = src[i + cd->sel_channel]; + destination = dst[j++]; + assert_int_equal(source_in, destination); + } + } + } +} + + +static void verify_s16le_2ch_to_2ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint16_t *src = (uint16_t *)source->r_ptr; + const uint16_t *dst = (uint16_t *)sink->w_ptr; + uint32_t channels = dev->params.channels; + uint32_t channel; + uint32_t i; + double processed; + + for (i = 0; i < sink->size / sizeof(uint16_t); i += channels) { + for (channel = 0; channel < channels; channel++) { + processed = src[i + channel]; + assert_int_equal(dst[i + channel], processed); + } + } +} + +static void verify_s16le_4ch_to_4ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint16_t *src = (uint16_t *)source->r_ptr; + const uint16_t *dst = (uint16_t *)sink->w_ptr; + uint32_t channels = dev->params.channels; + uint32_t channel; + uint32_t i; + double processed; + + for (i = 0; i < sink->size / sizeof(uint16_t); i += channels) { + for (channel = 0; channel < channels; channel++) { + processed = src[i + channel]; + assert_int_equal(dst[i + channel], processed); + } + } +} + +static void verify_s32le_2ch_to_2ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint32_t *src = (uint32_t *)source->r_ptr; + const uint32_t *dst = (uint32_t *)sink->w_ptr; + uint32_t channels = dev->params.channels; + uint32_t channel; + uint32_t i; + uint32_t processed; + + for (i = 0; i < sink->size / sizeof(uint32_t); i += channels) { + for (channel = 0; channel < channels; channel++) { + processed = src[i + channel]; + assert_int_equal(dst[i + channel], processed); + } + } +} + +static void verify_s32le_4ch_to_4ch(struct comp_dev *dev, struct comp_buffer *sink, + struct comp_buffer *source) +{ + struct comp_data *cd = comp_get_drvdata(dev); + const uint32_t *src = (uint32_t *)source->r_ptr; + const uint32_t *dst = (uint32_t *)sink->w_ptr; + uint32_t channels = dev->params.channels; + uint32_t channel; + uint32_t i; + uint32_t processed; + + for (i = 0; i < sink->size / sizeof(uint32_t); i += channels) { + for (channel = 0; channel < channels; channel++) { + processed = src[i + channel]; + assert_int_equal(dst[i + channel], processed); + } + } +} + +static void test_audio_sel(void **state) +{ + struct sel_test_state *sel_state = *state; + struct comp_data *cd = comp_get_drvdata(sel_state->dev); + + switch (cd->source_format) { + case SOF_IPC_FRAME_S16_LE: + fill_source_s16(sel_state); + break; + case SOF_IPC_FRAME_S24_4LE: + case SOF_IPC_FRAME_S32_LE: + case SOF_IPC_FRAME_FLOAT: + fill_source_s32(sel_state); + break; + } + + cd->sel_func(sel_state->dev, sel_state->sink, sel_state->source, + sel_state->dev->frames); + + sel_state->verify(sel_state->dev, sel_state->sink, sel_state->source); +} + + +static struct sel_test_parameters parameters[] = { + { 2, 1, 0, 16, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, + { 2, 1, 1, 16, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, + { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, + { 2, 1, 1, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, + { 2, 2, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_2ch_to_2ch }, + { 4, 4, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_4ch_to_4ch }, + { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, + { 4, 1, 0, 48, 1, SOF_IPC_FRAME_S16_LE, SOF_IPC_FRAME_S16_LE, verify_s16le_Xch_to_1ch }, + + { 2, 1, 0, 16, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, + { 2, 1, 1, 16, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, + { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, + { 2, 1, 1, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, + { 2, 2, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_2ch_to_2ch }, + { 4, 4, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_4ch_to_4ch }, + { 2, 1, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, + { 4, 1, 0, 48, 1, SOF_IPC_FRAME_S24_4LE, SOF_IPC_FRAME_S24_4LE, verify_s32le_Xch_to_1ch }, +}; + +int main(void) +{ + int i; + + struct CMUnitTest tests[ARRAY_SIZE(parameters)]; + + for (i = 0; i < ARRAY_SIZE(parameters); i++) { + tests[i].name = "test_audio_sel"; + tests[i].test_func = test_audio_sel; + tests[i].setup_func = setup; + tests[i].teardown_func = teardown; + tests[i].initial_state = ¶meters[i]; + } + + cmocka_set_message_output(CM_OUTPUT_TAP); + + return cmocka_run_group_tests(tests, NULL, NULL); +}