Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CTC binary and switch controls #9370

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 61 additions & 27 deletions src/audio/google/google_ctc_audio_processing.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <rtos/init.h>

#include <google_ctc_audio_processing.h>
#include <google_ctc_audio_processing_sof_message_reader.h>

#include "google_ctc_audio_processing.h"

Expand Down Expand Up @@ -62,6 +61,19 @@ static inline float convert_int32_to_float(int32_t data)
static const int kChunkFrames = 48;
static const int kMaxChannels = 2;

static void ctc_passthrough(const struct audio_stream *source,
struct audio_stream *sink,
struct input_stream_buffer *input_buffers,
struct output_stream_buffer *output_buffers,
uint32_t frames)
{
int n_ch = audio_stream_get_channels(source);
int samples = frames * n_ch;

audio_stream_copy(source, 0, sink, 0, samples);
module_update_buffer_position(&input_buffers[0], &output_buffers[0], frames);
}

#if CONFIG_FORMAT_S16LE
static void ctc_s16_default(struct google_ctc_audio_processing_comp_data *cd,
const struct audio_stream *source,
Expand All @@ -80,6 +92,11 @@ static void ctc_s16_default(struct google_ctc_audio_processing_comp_data *cd,
int samples_to_written = MIN(samples, audio_stream_samples_without_wrap_s16(sink, dest));
int written_samples = 0;

if (!cd->enabled) {
eddy1021 marked this conversation as resolved.
Show resolved Hide resolved
ctc_passthrough(source, sink, input_buffers, output_buffers, frames);
return;
}

// writes previous processed samples to the output.
while (cd->next_avail_output_samples < cd->chunk_frames * n_ch &&
written_samples < samples_to_written) {
Expand Down Expand Up @@ -130,6 +147,11 @@ static void ctc_s24_default(struct google_ctc_audio_processing_comp_data *cd,
int samples_to_written = MIN(samples, audio_stream_samples_without_wrap_s24(sink, dest));
int written_samples = 0;

if (!cd->enabled) {
ctc_passthrough(source, sink, input_buffers, output_buffers, frames);
return;
}

// writes previous processed samples to the output.
while (cd->next_avail_output_samples < cd->chunk_frames * n_ch &&
written_samples < samples_to_written) {
Expand Down Expand Up @@ -180,6 +202,11 @@ static void ctc_s32_default(struct google_ctc_audio_processing_comp_data *cd,
int samples_to_written = MIN(samples, audio_stream_samples_without_wrap_s32(sink, dest));
int written_samples = 0;

if (!cd->enabled) {
ctc_passthrough(source, sink, input_buffers, output_buffers, frames);
return;
}

// writes previous processed samples to the output.
while (cd->next_avail_output_samples < cd->chunk_frames * n_ch &&
written_samples < samples_to_written) {
Expand Down Expand Up @@ -216,6 +243,8 @@ static int ctc_free(struct processing_module *mod)
{
struct google_ctc_audio_processing_comp_data *cd = module_get_private_data(mod);

comp_info(mod->dev, "ctc_free()");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 3rd commit could have been a separate PR as well. Not a blocker.


if (cd) {
rfree(cd->input);
rfree(cd->output);
Expand Down Expand Up @@ -268,6 +297,8 @@ static int ctc_init(struct processing_module *mod)
return -ENOMEM;
}

cd->enabled = true;

comp_dbg(dev, "ctc_init(): Ready");

return 0;
Expand Down Expand Up @@ -298,31 +329,15 @@ static int google_ctc_audio_processing_reconfigure(struct processing_module *mod
config, size);

cd->reconfigure = false;

uint8_t *processing_config;
size_t processing_config_size;
bool processing_config_present;

GoogleCtcAudioProcessingParseSofConfigMessage(config, size,
&processing_config,
&processing_config_size,
&processing_config_present);

if (processing_config_present) {
comp_info(dev,
"google_ctc_audio_processing_reconfigure(): Applying config of size %zu bytes",
processing_config_size);

ret = GoogleCtcAudioProcessingReconfigure(cd->state,
processing_config,
processing_config_size);
if (ret) {
comp_err(dev, "GoogleCtcAudioProcessingReconfigure failed: %d",
ret);
return ret;
}
comp_info(dev,
"google_ctc_audio_processing_reconfigure(): Applying config of size %zu bytes",
size);
ret = GoogleCtcAudioProcessingReconfigure(cd->state, config, size);
if (ret) {
comp_err(dev, "GoogleCtcAudioProcessingReconfigure failed: %d",
ret);
return ret;
}

return 0;
}

Expand All @@ -335,6 +350,8 @@ static int ctc_prepare(struct processing_module *mod,
struct comp_buffer *source;
enum sof_ipc_frame fmt;
int num_channels;
uint8_t *config;
int config_size;

comp_info(mod->dev, "ctc_prepare()");

Expand Down Expand Up @@ -367,22 +384,39 @@ static int ctc_prepare(struct processing_module *mod,
}
cd->next_avail_output_samples = cd->chunk_frames * num_channels;

config = comp_get_data_blob(cd->tuning_handler, &config_size, NULL);

if (config_size != CTC_BLOB_CONFIG_SIZE) {
comp_info(mod->dev, "ctc_prepare(): config_size not expected: %d", config_size);
config = NULL;
config_size = 0;
}
cd->state = GoogleCtcAudioProcessingCreateWithConfig(cd->chunk_frames,
audio_stream_get_rate(&source->stream),
/*config=*/NULL,
/*config_size=*/0);
config,
eddy1021 marked this conversation as resolved.
Show resolved Hide resolved
config_size);
if (!cd->state) {
comp_err(mod->dev, "ctc_prepare(), failed to create CTC");
return -ENOMEM;
}

return 0;
}

static int ctc_reset(struct processing_module *mod)
{
struct google_ctc_audio_processing_comp_data *cd = module_get_private_data(mod);
size_t buf_size = cd->chunk_frames * sizeof(cd->input[0]) * kMaxChannels;

comp_info(mod->dev, "ctc_reset()");

GoogleCtcAudioProcessingFree(cd->state);
cd->state = NULL;
cd->ctc_func = NULL;
cd->input_samples = 0;
cd->next_avail_output_samples = 0;
memset(cd->input, 0, buf_size);
memset(cd->output, 0, buf_size);
return 0;
}

Expand Down
14 changes: 14 additions & 0 deletions src/audio/google/google_ctc_audio_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,24 @@ struct google_ctc_audio_processing_comp_data {
uint32_t chunk_frames;
GoogleCtcAudioProcessingState *state;
struct comp_data_blob_handler *tuning_handler;
bool enabled;
bool reconfigure;
ctc_func ctc_func;
};

struct google_ctc_config {
/* size of the whole ctc config */
uint32_t size;
eddy1021 marked this conversation as resolved.
Show resolved Hide resolved

/* reserved */
uint32_t reserved[4];
eddy1021 marked this conversation as resolved.
Show resolved Hide resolved

uint32_t data[];
} __packed;

#define CTC_BLOB_DATA_SIZE 4100
#define CTC_BLOB_CONFIG_SIZE (sizeof(struct google_ctc_config) + CTC_BLOB_DATA_SIZE)

int ctc_set_config(struct processing_module *mod, uint32_t param_id,
enum module_cfg_fragment_position pos,
uint32_t data_offset_size,
Expand Down
33 changes: 30 additions & 3 deletions src/audio/google/google_ctc_audio_processing_ipc3.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,45 @@ int ctc_set_config(struct processing_module *mod, uint32_t param_id,
struct sof_ipc_ctrl_data *cdata = (struct sof_ipc_ctrl_data *)fragment;
struct google_ctc_audio_processing_comp_data *cd = module_get_private_data(mod);
int ret;
struct google_ctc_config *config;
int size;

switch (cdata->cmd) {
case SOF_CTRL_CMD_BINARY:
ret = comp_data_blob_set_cmd(cd->tuning_handler, cdata);
if (ret)
return ret;
if (comp_is_new_data_blob_available(cd->tuning_handler)) {
comp_get_data_blob(cd->tuning_handler, NULL, NULL);
config = comp_get_data_blob(cd->tuning_handler, &size, NULL);
if (size != CTC_BLOB_CONFIG_SIZE) {
eddy1021 marked this conversation as resolved.
Show resolved Hide resolved
comp_err(mod->dev,
"ctc_set_config(): Invalid config size = %d",
size);
return -EINVAL;
}
if (config->size != CTC_BLOB_CONFIG_SIZE) {
comp_err(mod->dev,
"ctc_set_config(): Invalid config->size = %d",
config->size);
return -EINVAL;
}
cd->reconfigure = true;
}
return 0;
case SOF_CTRL_CMD_SWITCH:
eddy1021 marked this conversation as resolved.
Show resolved Hide resolved
if (cdata->num_elems == 1) {
cd->enabled = cdata->chanv[0].value;
comp_info(dev, "ctc_set_config(), enabled = %d",
cd->enabled);
return 0;
}
eddy1021 marked this conversation as resolved.
Show resolved Hide resolved
comp_err(mod->dev,
"ctc_set_config(): Illegal num_elems = %d",
cdata->num_elems);
return -EINVAL;
default:
comp_err(mod->dev,
"google_ctc_audio_processing_ctrl_set_data(): Only binary controls supported %d",
"ctc_set_config(): Only binary and switch controls supported %d",
cdata->cmd);
return -EINVAL;
}
Expand All @@ -54,11 +79,13 @@ int ctc_get_config(struct processing_module *mod,
struct sof_ipc_ctrl_data *cdata = (struct sof_ipc_ctrl_data *)fragment;
struct google_ctc_audio_processing_comp_data *cd = module_get_private_data(mod);

comp_info(mod->dev, "google_ctc_audio_processing_ctrl_get_data(): %u", cdata->cmd);
comp_info(mod->dev, "ctc_get_config(): %u", cdata->cmd);

switch (cdata->cmd) {
case SOF_CTRL_CMD_BINARY:
return comp_data_blob_get_cmd(cd->tuning_handler, cdata, fragment_size);
case SOF_CTRL_CMD_SWITCH:
return comp_data_blob_get_cmd(cd->tuning_handler, cdata, fragment_size);
default:
return -EINVAL;
}
Expand Down
35 changes: 31 additions & 4 deletions src/audio/google/google_ctc_audio_processing_ipc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,27 @@ int ctc_set_config(struct processing_module *mod, uint32_t param_id,
size_t fragment_size, uint8_t *response,
size_t response_size)
{
struct sof_ipc4_control_msg_payload *ctl = (struct sof_ipc4_control_msg_payload *)fragment;
struct google_ctc_audio_processing_comp_data *cd = module_get_private_data(mod);
int ret;
struct google_ctc_config *config;
int size;

switch (param_id) {
case 0:
break;
case SOF_IPC4_SWITCH_CONTROL_PARAM_ID:
if (ctl->id == 0 && ctl->num_elems == 1) {
cd->enabled = ctl->chanv[0].value;
comp_info(mod->dev, "ctc_set_config(): enabled = %d", cd->enabled);
return 0;
}
comp_err(mod->dev, "ctc_set_config(): Illegal control id = %d, num_elems = %d",
ctl->id, ctl->num_elems);
return -EINVAL;
case SOF_IPC4_ENUM_CONTROL_PARAM_ID:
default:
comp_err(mod->dev, "google_ctc_audio_processing_set_data(): Only binary controls supported");
comp_err(mod->dev, "ctc_set_config(): Only binary and switch controls supported");
return -EINVAL;
}

Expand All @@ -44,7 +55,19 @@ int ctc_set_config(struct processing_module *mod, uint32_t param_id,
return ret;

if (comp_is_new_data_blob_available(cd->tuning_handler)) {
comp_get_data_blob(cd->tuning_handler, NULL, NULL);
config = comp_get_data_blob(cd->tuning_handler, &size, NULL);
if (size != CTC_BLOB_CONFIG_SIZE) {
comp_err(mod->dev,
"ctc_set_config(): Invalid config size = %d",
size);
return -EINVAL;
}
if (config->size != CTC_BLOB_CONFIG_SIZE) {
comp_err(mod->dev,
"ctc_set_config(): Invalid config->size = %d",
config->size);
return -EINVAL;
}
cd->reconfigure = true;
}

Expand All @@ -55,6 +78,10 @@ int ctc_get_config(struct processing_module *mod,
uint32_t param_id, uint32_t *data_offset_size,
uint8_t *fragment, size_t fragment_size)
{
comp_err(mod->dev, "ctc_get_config(): Not supported");
return -EINVAL;
struct sof_ipc_ctrl_data *cdata = (struct sof_ipc_ctrl_data *)fragment;
struct google_ctc_audio_processing_comp_data *cd = module_get_private_data(mod);

comp_info(mod->dev, "ctc_get_config(): %u", cdata->cmd);

return comp_data_blob_get_cmd(cd->tuning_handler, cdata, fragment_size);
}
11 changes: 0 additions & 11 deletions src/audio/google/google_ctc_audio_processing_mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,6 @@ void GoogleCtcAudioProcessingProcess(GoogleCtcAudioProcessingState *state,
src, sizeof(float) * num_frames * num_channels);
}

void GoogleCtcAudioProcessingParseSofConfigMessage(uint8_t *message,
size_t message_size,
uint8_t **config,
size_t *config_size,
bool *config_present)
{
*config = NULL;
*config_size = 0;
*config_present = false;
}

int GoogleCtcAudioProcessingReconfigure(GoogleCtcAudioProcessingState *state,
const uint8_t *config, int config_size)
{
Expand Down

This file was deleted.

12 changes: 9 additions & 3 deletions tools/topology/topology2/cavs-mixin-mixout-ctc-ssp.conf
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ Object.Pipeline {
}
}
Object.Widget.ctc.1 {
Object.Control.bytes."1" {
name 'CTC.0'
}
Object.Control {
mixer."1" {
name 'CTC Switch'
}
bytes."1" {
name 'CTC.0'
max 4160
}
}
}
}
]
Expand Down
Loading
Loading