From 8fc259f26ce79d4b6aaf67204f073190e0f0848a Mon Sep 17 00:00:00 2001 From: Daniel Baluta Date: Fri, 21 Jan 2022 16:31:09 +0200 Subject: [PATCH 1/5] codec_adapter: cadence: Introduce cadence_codec_post_init This new function encapsulates all Cadence API calls at init time. We introduce this new function because the init API calls need to be done after we figure out cadence codec api id. Right now cadence codec api id is hardcoded to DEFAULT_CODEC_ID but later it will be needed to be determined dyncamically at runtime. It will be sent from host via sof_ipc_stream_params. Signed-off-by: Daniel Baluta --- src/audio/codec_adapter/codec/cadence.c | 79 +++++++++++++++---------- 1 file changed, 47 insertions(+), 32 deletions(-) diff --git a/src/audio/codec_adapter/codec/cadence.c b/src/audio/codec_adapter/codec/cadence.c index d215e68cb8f4..5013f3f1e4fe 100644 --- a/src/audio/codec_adapter/codec/cadence.c +++ b/src/audio/codec_adapter/codec/cadence.c @@ -94,12 +94,56 @@ static struct cadence_api cadence_api_table[] = { #endif }; +static int cadence_codec_post_init(struct comp_dev *dev) +{ + struct module_data *codec = comp_get_module_data(dev); + struct cadence_codec_data *cd = codec->private; + uint32_t obj_size; + int ret; + + comp_dbg(dev, "cadence_codec_post_init() start"); + + /* Obtain codec name */ + API_CALL(cd, XA_API_CMD_GET_LIB_ID_STRINGS, + XA_CMD_TYPE_LIB_NAME, cd->name, ret); + if (ret != LIB_NO_ERROR) { + comp_err(dev, "cadence_codec_post_init() error %x: failed to get lib name", ret); + return ret; + } + + /* Get codec object size */ + API_CALL(cd, XA_API_CMD_GET_API_SIZE, 0, &obj_size, ret); + if (ret != LIB_NO_ERROR) { + comp_err(dev, "cadence_codec_post_init() error %x: failed to get lib object size", + ret); + return ret; + } + /* Allocate space for codec object */ + cd->self = rballoc(0, SOF_MEM_CAPS_RAM, obj_size); + if (!cd->self) { + comp_err(dev, "cadence_codec_post_init(): failed to allocate space for lib object"); + return -ENOMEM; + } + + comp_dbg(dev, "cadence_codec_post_init(): allocated %d bytes for lib object", obj_size); + + /* Set all params to their default values */ + API_CALL(cd, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_API_PRE_CONFIG_PARAMS, NULL, ret); + if (ret != LIB_NO_ERROR) { + rfree(cd->self); + return ret; + } + + comp_dbg(dev, "cadence_codec_post_init() done"); + + return 0; +} + static int cadence_codec_init(struct comp_dev *dev) { int ret; struct module_data *codec = comp_get_module_data(dev); struct cadence_codec_data *cd = NULL; - uint32_t obj_size; uint32_t no_of_api = ARRAY_SIZE(cadence_api_table); uint32_t api_id = CODEC_GET_API_ID(DEFAULT_CODEC_ID); uint32_t i; @@ -160,38 +204,9 @@ static int cadence_codec_init(struct comp_dev *dev) setup_cfg->avail = true; } - /* Obtain codec name */ - API_CALL(cd, XA_API_CMD_GET_LIB_ID_STRINGS, - XA_CMD_TYPE_LIB_NAME, cd->name, ret); - if (ret != LIB_NO_ERROR) { - comp_err(dev, "cadence_codec_init() error %x: failed to get lib name", - ret); - goto free; - } - /* Get codec object size */ - API_CALL(cd, XA_API_CMD_GET_API_SIZE, 0, &obj_size, ret); - if (ret != LIB_NO_ERROR) { - comp_err(dev, "cadence_codec_init() error %x: failed to get lib object size", - ret); - goto free; - } - /* Allocate space for codec object */ - cd->self = rballoc(0, SOF_MEM_CAPS_RAM, obj_size); - if (!cd->self) { - comp_err(dev, "cadence_codec_init(): failed to allocate space for lib object"); - ret = -ENOMEM; - goto free; - } - - comp_dbg(dev, "cadence_codec_init(): allocated %d bytes for lib object", obj_size); - - /* Set all params to their default values */ - API_CALL(cd, XA_API_CMD_INIT, XA_CMD_TYPE_INIT_API_PRE_CONFIG_PARAMS, - NULL, ret); - if (ret != LIB_NO_ERROR) { - rfree(cd->self); + ret = cadence_codec_post_init(dev); + if (ret < 0) goto free; - } comp_dbg(dev, "cadence_codec_init() done"); From 186b86eb82f729e19ddbf69aeb4ed367ae90cb6a Mon Sep 17 00:00:00 2001 From: Daniel Baluta Date: Fri, 21 Jan 2022 16:42:48 +0200 Subject: [PATCH 2/5] codec_adapter: cadence: Introduce cadence_codec_resolve_api Separate code that finds and assigns API function given an codec_id into a function. This makes code cleaner and helps us move finding the api id in another part of the code. Signed-off-by: Daniel Baluta --- src/audio/codec_adapter/codec/cadence.c | 48 +++++++++++++++---------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/audio/codec_adapter/codec/cadence.c b/src/audio/codec_adapter/codec/cadence.c index 5013f3f1e4fe..ee249f429f99 100644 --- a/src/audio/codec_adapter/codec/cadence.c +++ b/src/audio/codec_adapter/codec/cadence.c @@ -94,6 +94,34 @@ static struct cadence_api cadence_api_table[] = { #endif }; +static int cadence_codec_resolve_api(struct comp_dev *dev, uint32_t api_id) +{ + struct module_data *codec = comp_get_module_data(dev); + uint32_t no_of_api = ARRAY_SIZE(cadence_api_table); + struct cadence_codec_data *cd = codec->private; + int i; + + /* Find and assign API function */ + for (i = 0; i < no_of_api; i++) { + if (cadence_api_table[i].id == api_id) { + cd->api = cadence_api_table[i].api; + break; + } + } + + cd->api_id = api_id; + + /* Verify API assignment */ + if (!cd->api) { + comp_err(dev, "cadence_codec_init(): could not find API function for id %x", + api_id); + return -EINVAL; + } + + return 0; +} + + static int cadence_codec_post_init(struct comp_dev *dev) { struct module_data *codec = comp_get_module_data(dev); @@ -144,9 +172,7 @@ static int cadence_codec_init(struct comp_dev *dev) int ret; struct module_data *codec = comp_get_module_data(dev); struct cadence_codec_data *cd = NULL; - uint32_t no_of_api = ARRAY_SIZE(cadence_api_table); uint32_t api_id = CODEC_GET_API_ID(DEFAULT_CODEC_ID); - uint32_t i; comp_dbg(dev, "cadence_codec_init() start"); @@ -163,23 +189,9 @@ static int cadence_codec_init(struct comp_dev *dev) cd->api = NULL; cd->setup_cfg.avail = false; - /* Find and assign API function */ - for (i = 0; i < no_of_api; i++) { - if (cadence_api_table[i].id == api_id) { - cd->api = cadence_api_table[i].api; - break; - } - } - - cd->api_id = api_id; - - /* Verify API assignment */ - if (!cd->api) { - comp_err(dev, "cadence_codec_init(): could not find API function for id %x", - api_id); - ret = -EINVAL; + ret = cadence_codec_resolve_api(dev, api_id); + if (ret < 0) goto free; - } /* copy the setup config only for the first init */ if (codec->state == MODULE_DISABLED && codec->cfg.avail) { From cb7db1639ced5d75c931de16f69307c3e2dc0b76 Mon Sep 17 00:00:00 2001 From: Daniel Baluta Date: Fri, 21 Jan 2022 16:51:19 +0200 Subject: [PATCH 3/5] codec_adapter: cadence: Move cadence_resolve_api Move cadence_codec_resolve_api to cadence_codec_post_init function. We need this because later the entire cadence_codec_post_init function will be called later then init. At init we do not know yet the codec id. Signed-off-by: Daniel Baluta --- src/audio/codec_adapter/codec/cadence.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/audio/codec_adapter/codec/cadence.c b/src/audio/codec_adapter/codec/cadence.c index ee249f429f99..267c00d77de3 100644 --- a/src/audio/codec_adapter/codec/cadence.c +++ b/src/audio/codec_adapter/codec/cadence.c @@ -124,6 +124,7 @@ static int cadence_codec_resolve_api(struct comp_dev *dev, uint32_t api_id) static int cadence_codec_post_init(struct comp_dev *dev) { + uint32_t api_id = CODEC_GET_API_ID(DEFAULT_CODEC_ID); struct module_data *codec = comp_get_module_data(dev); struct cadence_codec_data *cd = codec->private; uint32_t obj_size; @@ -131,6 +132,10 @@ static int cadence_codec_post_init(struct comp_dev *dev) comp_dbg(dev, "cadence_codec_post_init() start"); + ret = cadence_codec_resolve_api(dev, api_id); + if (ret < 0) + return ret; + /* Obtain codec name */ API_CALL(cd, XA_API_CMD_GET_LIB_ID_STRINGS, XA_CMD_TYPE_LIB_NAME, cd->name, ret); @@ -172,7 +177,6 @@ static int cadence_codec_init(struct comp_dev *dev) int ret; struct module_data *codec = comp_get_module_data(dev); struct cadence_codec_data *cd = NULL; - uint32_t api_id = CODEC_GET_API_ID(DEFAULT_CODEC_ID); comp_dbg(dev, "cadence_codec_init() start"); @@ -189,10 +193,6 @@ static int cadence_codec_init(struct comp_dev *dev) cd->api = NULL; cd->setup_cfg.avail = false; - ret = cadence_codec_resolve_api(dev, api_id); - if (ret < 0) - goto free; - /* copy the setup config only for the first init */ if (codec->state == MODULE_DISABLED && codec->cfg.avail) { struct module_config *setup_cfg = &cd->setup_cfg; From 6244f8309bce8da88efb02612f930aa4d3233970 Mon Sep 17 00:00:00 2001 From: Daniel Baluta Date: Fri, 21 Jan 2022 16:55:56 +0200 Subject: [PATCH 4/5] codec_adapter: cadence: Call cadence_codec_post_init later We need to call cadence_codec_post_init later at a point where the codec_id is know. Best possible location is cadence_codec_prepare. By this time the sof_ipc_stream_params should have already been sent to the DSP by the Host. Signed-off-by: Daniel Baluta --- src/audio/codec_adapter/codec/cadence.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/audio/codec_adapter/codec/cadence.c b/src/audio/codec_adapter/codec/cadence.c index 267c00d77de3..7532439a3711 100644 --- a/src/audio/codec_adapter/codec/cadence.c +++ b/src/audio/codec_adapter/codec/cadence.c @@ -216,16 +216,9 @@ static int cadence_codec_init(struct comp_dev *dev) setup_cfg->avail = true; } - ret = cadence_codec_post_init(dev); - if (ret < 0) - goto free; - comp_dbg(dev, "cadence_codec_init() done"); return 0; -free: - rfree(cd); - return ret; } static int cadence_codec_apply_config(struct comp_dev *dev) @@ -456,6 +449,13 @@ static int cadence_codec_prepare(struct comp_dev *dev) comp_dbg(dev, "cadence_codec_prepare() start"); + ret = cadence_codec_post_init(dev); + if (ret < 0) { + comp_err(dev, "cadence_codec_prepare() error %x: failed post init", + ret); + return ret; + } + ret = cadence_codec_apply_config(dev); if (ret) { comp_err(dev, "cadence_codec_prepare() error %x: failed to apply config", From 67cf9478ae93956949a016b3cd829e3a48379b3b Mon Sep 17 00:00:00 2001 From: Daniel Baluta Date: Fri, 21 Jan 2022 17:10:17 +0200 Subject: [PATCH 5/5] stream: Add codec_id field in ipc_params With this we can select a given codec from a family of codecs. E.g Cadence supports (DAP, MP3, AAC) and codec_id will help us identify at runtime which codec api function to use. Signed-off-by: Daniel Baluta --- src/include/ipc/stream.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/include/ipc/stream.h b/src/include/ipc/stream.h index 1bf75832d5dc..101facb049e8 100644 --- a/src/include/ipc/stream.h +++ b/src/include/ipc/stream.h @@ -95,7 +95,9 @@ struct sof_ipc_stream_params { uint32_t host_period_bytes; uint16_t no_stream_position; /**< 1 means don't send stream position */ - uint16_t reserved[3]; + uint8_t codec_id; + + uint8_t reserved[5]; uint16_t chmap[SOF_IPC_MAX_CHANNELS]; /**< channel map - SOF_CHMAP_ */ } __attribute__((packed, aligned(4)));