Add raw OPUS codec support for compress offload
Subsystems (5)
v3 cover Add raw OPUS codec support for compress offload
This series adds support in kernel to recognise raw (or plain) OPUS codec playback for compress offloading. At this point this series doesn't deal with OPUS packets packed in any kind of containers (OGG or others) and focuses on adding missing bits for pure OPUS packets.
The second patch adds its usage in Qualcomm Audio DSP code. To correctly recognise raw OPUS packets by qdsp6, each packets needs to be prepended with 4-bytes field that contains length of a raw OPUS packet. It is expected to be useful for usecases when OPUS packets are streamed over network and they are not encapsulated in a container. Userspace application that will use the compress API has to manually add such 4-bytes long field to each OPUS packet.
This is tested on sm8750-mtp. It is expected that next hardware revisions will also support raw OPUS codec offloading.
Dependencies are:
-- hardware with DSP that supports decoding OPUS packets (>= Qualcomm sm8750); -- adsp fastrpc;
-- explicitly setting format in sm8750 soundcard driver
https://lore.kernel.org/linux-sound/[email protected]/
-- running adsprpcd tool with support for Audio PD and DSP libraries loading support (or its alternative);
-- tinycompress fcplay tool that will prepare raw opus packets and do the required addition of length field;
-- mfc module in topology compress-playback path, that module is expected to parse channel mapping and do the required things to map streams/channels to physically-present output channels on backend.
The userspace tinycompress tool with support for raw OPUS compress playback is located here:
https://github.com/laklimov/tinycompress_opus
branch: opus_v3_workinprogress
The userspace tool is not expected that it is ready and still needs some work, for instance recognition that it runs on Qualcomm hardware. More like working PoC.
---
Changes in v3:
- back to define version member as __u8 in snd_dec_opus and reading it in audioreach_set_compr_media_format() as suggested by Takashi;
- new patch, bumping compress API minor version as suggested by Vinod and Takashi;
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2 (as suggested by Vinod):
- opus decoder struct: switch version memeber to be union to allow reading it
as a byte or two 4-bits fields;
- audioreach_set_compr_media_format: read version byte.
- Link to v1: https://lore.kernel.org/r/[email protected]
Changes since RFC (mostly as suggetsed by Srini):
- added struct snd_dec_opus_ch_map to struct snd_dec_opus;
- changed tags;
- reworked comments for struct snd_dec_opus;
In qcom audioreach patch:
- corrected how {stream_count,coupled_count} is ejected from mcfg->codec;
- stopped dealing with channel mapping parsing (this is internal struct for decoder on audio DSP side);
- used sizeof(*opus_cfg) for payload size;
Tinycompress/fcplay:
- small updates to match kernel structs.
- Link to RFC: https://lore.kernel.org/r/[email protected]
---
Alexey Klimov (3):
ALSA: compress: add raw opus codec define and opus decoder structs
ALSA: compress_offload: increase SNDRV_COMPRESS_VERSION minor version by 1
ASoC: qcom: qdsp6/audioreach: add support for offloading raw opus playback
include/uapi/sound/compress_offload.h | 2 +- include/uapi/sound/compress_params.h | 43 ++++++++++++++++++++++++++++++++++- sound/soc/qcom/qdsp6/audioreach.c | 27 ++++++++++++++++++++++ sound/soc/qcom/qdsp6/audioreach.h | 17 ++++++++++++++ sound/soc/qcom/qdsp6/q6apm-dai.c | 3 ++- sound/soc/qcom/qdsp6/q6apm.c | 3 +++ 6 files changed, 92 insertions(+), 3 deletions(-)
---
base-commit: 05af764719214d6568adb55c8749dec295228da8
change-id: 20250616-opus_codec_rfc_v1-b60bd308893b
Best regards,
Signature · 1 line
Alexey Klimov <[email protected]>
PATCH v3 1/3 ALSA: compress: add raw opus codec define and opus decoder structs
Adds a raw opus codec define and raw opus decoder structs. This is for raw OPUS packets not packed in any type of container (for instance OGG container). The decoder struct fields are taken from corresponding RFC document: RFC 7845 Section 5.
--- include/uapi/sound/compress_params.h | 43 +++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)
include/uapi/sound/compress_params.h +42 -1
@@ -43,7 +43,8 @@#define SND_AUDIOCODEC_BESPOKE ((__u32) 0x0000000E)#define SND_AUDIOCODEC_ALAC ((__u32) 0x0000000F)#define SND_AUDIOCODEC_APE ((__u32) 0x00000010) -#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_APE +#define SND_AUDIOCODEC_OPUS_RAW ((__u32) 0x00000011) +#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_OPUS_RAW/* * Profile and modes are listed with bit masks. This allows for a
@@ -324,6 +325,45 @@ struct snd_dec_ape {__u32 seek_table_present; } __attribute__((packed, aligned(4))); +/** +* struct snd_dec_opus - Opus decoder parameters (raw opus packets) +* @version: Usually should be '1' but can be split into major (4 upper bits) +* and minor (4 lower bits) sub-fields. +* @num_channels: Number of output channels. +* @pre_skip: Number of samples to discard at 48 kHz. +* @sample_rate: Sample rate of original input. +* @output_gain: Gain to apply when decoding (in Q7.8 format). +* @mapping_family: Order and meaning of output channels. Only values 0 and 1 +* are expected; values 2..255 are not recommended for playback. +* +* Optional channel mapping table. Describes mapping of opus streams to decoded +* channels. +* @struct snd_dec_opus_ch_map +* @stream_count: Number of streams encoded in each Ogg packet. +* @coupled_count: Number of streams whose decoders are used for two +* channels. +* @channel_map: describes which decoded channel to be used for each one. +* See RFC doc for details. +* This supports only mapping families 0 and 1, therefore max +* number of channels is 8. +* +* These options were extracted from RFC7845 Section 5. +*/ + +struct snd_dec_opus { +__u8 version; +__u8 num_channels; +__u16 pre_skip; +__u32 sample_rate; +__u16 output_gain; +__u8 mapping_family; +struct snd_dec_opus_ch_map { +__u8 stream_count; +__u8 coupled_count; +__u8 channel_map[8 ]; + } chan_map; +} __attribute__((packed,aligned (4 ))); +union snd_codec_options {struct snd_enc_wma wma;struct snd_enc_vorbis vorbis;
@@ -334,6 +374,7 @@ union snd_codec_options {struct snd_dec_wma wma_d;struct snd_dec_alac alac_d;struct snd_dec_ape ape_d; +struct snd_dec_opus opus_d;struct {__u32 out_sample_rate; } src_d;
Signature · 1 line
2.47.3
Vinod Koul
maintainerRe: [PATCH v3 1/3] ALSA: compress: add raw opus codec define and opus decoder structs
Quoted 29 lines · Alexey Klimov
Adds a raw opus codec define and raw opus decoder structs. This is for raw OPUS packets not packed in any type of container (for instance OGG container). The decoder struct fields are taken from corresponding RFC document: RFC 7845 Section 5.
Cc Srinivas Kandagatla <[email protected]>Cc Vinod Koul <[email protected]>Co-developed-by Annemarie Porter <[email protected]>Signed-off-by Annemarie Porter <[email protected]>Signed-off-by Alexey Klimov <[email protected]>--- include/uapi/sound/compress_params.h | 43 +++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)include/uapi/sound/compress_params.h +2
-1@@ -43,7 +43,8 @@#define SND_AUDIOCODEC_BESPOKE ((__u32) 0x0000000E)#define SND_AUDIOCODEC_ALAC ((__u32) 0x0000000F)#define SND_AUDIOCODEC_APE ((__u32) 0x00000010) -#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_APE +#define SND_AUDIOCODEC_OPUS_RAW ((__u32) 0x00000011) +#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_OPUS_RAW/* * Profile and modes are listed with bit masks. This allows for a
include/uapi/sound/compress_params.h struct snd_dec_ape { · line 331
Quoted 7 lines
include/uapi/sound/compress_params.h +4
-0@@ -324,6 +325,45 @@ struct snd_dec_ape {__u32 seek_table_present; } __attribute__((packed, aligned(4))); +/** +* struct snd_dec_opus - Opus decoder parameters (raw opus packets) +* @version: Usually should be '1' but can be split into major (4 upper bits) +* and minor (4 lower bits) sub-fields.
Please clarify, if that should be 1.0 so a value of 0x10
Quoted 49 lines
include/uapi/sound/compress_params.h +36
-0+* @num_channels: Number of output channels. +* @pre_skip: Number of samples to discard at 48 kHz. +* @sample_rate: Sample rate of original input. +* @output_gain: Gain to apply when decoding (in Q7.8 format). +* @mapping_family: Order and meaning of output channels. Only values 0 and 1 +* are expected; values 2..255 are not recommended for playback. +* +* Optional channel mapping table. Describes mapping of opus streams to decoded +* channels. +* @struct snd_dec_opus_ch_map +* @stream_count: Number of streams encoded in each Ogg packet. +* @coupled_count: Number of streams whose decoders are used for two +* channels. +* @channel_map: describes which decoded channel to be used for each one. +* See RFC doc for details. +* This supports only mapping families 0 and 1, therefore max +* number of channels is 8. +* +* These options were extracted from RFC7845 Section 5. +*/ + +struct snd_dec_opus { +__u8 version; +__u8 num_channels; +__u16 pre_skip; +__u32 sample_rate; +__u16 output_gain; +__u8 mapping_family; +struct snd_dec_opus_ch_map { +__u8 stream_count; +__u8 coupled_count; +__u8 channel_map[8 ]; + } chan_map; +} __attribute__((packed,aligned (4 ))); +union snd_codec_options {struct snd_enc_wma wma;struct snd_enc_vorbis vorbis;@@ -334,6 +374,7 @@ union snd_codec_options {struct snd_dec_wma wma_d;struct snd_dec_alac alac_d;struct snd_dec_ape ape_d; +struct snd_dec_opus opus_d;struct {__u32 out_sample_rate; } src_d;Signature · 2 lines
-- 2.47.3
Signature · 1 line
~Vinod
Re: [PATCH v3 1/3] ALSA: compress: add raw opus codec define and opus decoder structs
Quoted 33 lines · Vinod Koul
Quoted 32 lines · Alexey Klimov
Adds a raw opus codec define and raw opus decoder structs. This is for raw OPUS packets not packed in any type of container (for instance OGG container). The decoder struct fields are taken from corresponding RFC document: RFC 7845 Section 5.
Cc Srinivas Kandagatla <[email protected]>Cc Vinod Koul <[email protected]>Co-developed-by Annemarie Porter <[email protected]>Signed-off-by Annemarie Porter <[email protected]>Signed-off-by Alexey Klimov <[email protected]>--- include/uapi/sound/compress_params.h | 43 +++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-)include/uapi/sound/compress_params.h +2
-1@@ -43,7 +43,8 @@#define SND_AUDIOCODEC_BESPOKE ((__u32) 0x0000000E)#define SND_AUDIOCODEC_ALAC ((__u32) 0x0000000F)#define SND_AUDIOCODEC_APE ((__u32) 0x00000010) -#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_APE +#define SND_AUDIOCODEC_OPUS_RAW ((__u32) 0x00000011) +#define SND_AUDIOCODEC_MAX SND_AUDIOCODEC_OPUS_RAW/* * Profile and modes are listed with bit masks. This allows for a @@ -324,6 +325,45 @@ struct snd_dec_ape {__u32 seek_table_present; } __attribute__((packed, aligned(4)));
Quoted 6 lines
include/uapi/sound/compress_params.h struct snd_dec_ape { · line 331
Quoted 4 lines
include/uapi/sound/compress_params.h +4
-0+/** +* struct snd_dec_opus - Opus decoder parameters (raw opus packets) +* @version: Usually should be '1' but can be split into major (4 upper bits) +* and minor (4 lower bits) sub-fields. Please clarify, if that should be 1.0 so a value of
0x10
Sorry for the delay.
So the spec says that the value must be '1' for 8-bit field for that version of specification which it seems it should be 0x1. The spliting into two 4-bit fields is for backward compatibility and in such case any version in minor fields will be accepted (15 or less). Anything starting with version 16 or 0x10 is treated as incompatible version for this version of spec.
Well, at least that's how I understand the spec.
Value 0x10 in this case seems to signal incompatibility unless I am missing something?
Thanks,
Alexey
PATCH v3 2/3 ALSA: compress_offload: increase SNDRV_COMPRESS_VERSION minor version by 1
In reply to Alexey KlimovSince addition of raw opus codec support we need to update compress API minor version by one.
Bump the SNDRV_COMPRESS_VERSION to 0.4.1.
--- include/uapi/sound/compress_offload.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/sound/compress_offload.h +1 -1
@@ -13,7 +13,7 @@#include <sound/asound.h> #include <sound/compress_params.h> -#define SNDRV_COMPRESS_VERSION SNDRV_PROTOCOL_VERSION(0, 4, 0) +#define SNDRV_COMPRESS_VERSION SNDRV_PROTOCOL_VERSION(0, 4, 1)/** * struct snd_compressed_buffer - compressed buffer * @fragment_size: size of buffer fragment in bytes
Signature · 1 line
2.47.3
Re: [PATCH v3 2/3] ALSA: compress_offload: increase SNDRV_COMPRESS_VERSION minor version by 1
Quoted 3 lines · Alexey Klimov
Since addition of raw opus codec support we need to update compress API minor version by one.
Bump theSNDRV_COMPRESS_VERSIONto 0.4.1.
Signature · 1 line
~Vinod
PATCH v3 3/3 ASoC: qcom: qdsp6/audioreach: add support for offloading raw opus playback
In reply to Alexey KlimovAdd support for OPUS module, OPUS format ID, media format payload struct and make it all recognizable by audioreach compress playback path.
At this moment this only supports raw or plain OPUS packets not encapsulated in container (for instance OGG container). For this usecase each OPUS packet needs to be prepended with 4-bytes long length field which is expected to be done by userspace applications. This is Qualcomm DSP specific requirement.
--- sound/soc/qcom/qdsp6/audioreach.c | 27 +++++++++++++++++++++++++++ sound/soc/qcom/qdsp6/audioreach.h | 17 +++++++++++++++++ sound/soc/qcom/qdsp6/q6apm-dai.c | 3 ++- sound/soc/qcom/qdsp6/q6apm.c | 3 +++ 4 files changed, 49 insertions(+), 1 deletion(-)
sound/soc/qcom/qdsp6/audioreach.c +27 -0
@@ -883,6 +883,7 @@ static int audioreach_set_compr_media_format(struct media_format *media_fmt_hdr,struct payload_media_fmt_aac_t *aac_cfg;struct payload_media_fmt_pcm *mp3_cfg;struct payload_media_fmt_flac_t *flac_cfg; +struct payload_media_fmt_opus_t *opus_cfg;switch (mcfg->fmt) {case SND_AUDIOCODEC_MP3 :
@@ -925,6 +926,32 @@ static int audioreach_set_compr_media_format(struct media_format *media_fmt_hdr, flac_cfg->min_frame_size = mcfg->codec.options.flac_d.min_frame_size; flac_cfg->max_frame_size = mcfg->codec.options.flac_d.max_frame_size;break ; +case SND_AUDIOCODEC_OPUS_RAW : + media_fmt_hdr->data_format =DATA_FORMAT_RAW_COMPRESSED ; + media_fmt_hdr->fmt_id =MEDIA_FMT_ID_OPUS ; + media_fmt_hdr->payload_size =sizeof (*opus_cfg); + p = p +sizeof (*media_fmt_hdr); + opus_cfg = p; +/* raw opus packets prepended with 4 bytes of length */ + opus_cfg->bitstream_format =1 ; +/* +* payload_type: +* 0 -- read metadata from opus stream; +* 1 -- metadata is provided by filling in the struct here. +*/ + opus_cfg->payload_type =1 ; + opus_cfg->version = mcfg->codec.options.opus_d.version; + opus_cfg->num_channels = mcfg->codec.options.opus_d.num_channels; + opus_cfg->pre_skip = mcfg->codec.options.opus_d.pre_skip; + opus_cfg->sample_rate = mcfg->codec.options.opus_d.sample_rate; + opus_cfg->output_gain = mcfg->codec.options.opus_d.output_gain; + opus_cfg->mapping_family = mcfg->codec.options.opus_d.mapping_family; + opus_cfg->stream_count = mcfg->codec.options.opus_d.chan_map.stream_count; + opus_cfg->coupled_count = mcfg->codec.options.opus_d.chan_map.coupled_count; +memcpy (opus_cfg->channel_mapping, mcfg->codec.options.opus_d.chan_map.channel_map, +sizeof (opus_cfg->channel_mapping)); + opus_cfg->reserved[0 ] = opus_cfg->reserved[1 ] = opus_cfg->reserved[2 ] =0 ; +break ;default :return -EINVAL ; }
sound/soc/qcom/qdsp6/audioreach.h +17 -0
@@ -31,6 +31,7 @@ struct q6apm_graph;#define MODULE_ID_MP3_DECODE 0x0700103B#define MODULE_ID_GAPLESS 0x0700104D#define MODULE_ID_DISPLAY_PORT_SINK 0x07001069 +#define MODULE_ID_OPUS_DEC 0x07001174#define APM_CMD_GET_SPF_STATE 0x01001021#define APM_CMD_RSP_GET_SPF_STATE 0x02001007
@@ -257,6 +258,22 @@ struct payload_media_fmt_aac_t {uint32_t sample_rate; } __packed; +#define MEDIA_FMT_ID_OPUS 0x09001039 +struct payload_media_fmt_opus_t { +uint16_t bitstream_format; +uint16_t payload_type; +uint8_t version; +uint8_t num_channels; +uint16_t pre_skip; +uint32_t sample_rate; +uint16_t output_gain; +uint8_t mapping_family; +uint8_t stream_count; +uint8_t coupled_count; +uint8_t channel_mapping[8 ]; +uint8_t reserved[3 ]; +} __packed; +#define DATA_CMD_WR_SH_MEM_EP_EOS 0x04001002#define WR_SH_MEM_EP_EOS_POLICY_LAST 1#define WR_SH_MEM_EP_EOS_POLICY_EACH 2
sound/soc/qcom/qdsp6/q6apm-dai.c +2 -1
@@ -551,10 +551,11 @@ static int q6apm_dai_compr_get_caps(struct snd_soc_component *component, caps->max_fragment_size =COMPR_PLAYBACK_MAX_FRAGMENT_SIZE ; caps->min_fragments =COMPR_PLAYBACK_MIN_NUM_FRAGMENTS ; caps->max_fragments =COMPR_PLAYBACK_MAX_NUM_FRAGMENTS ; - caps->num_codecs =3 ; + caps->num_codecs =4 ; caps->codecs[0 ] =SND_AUDIOCODEC_MP3 ; caps->codecs[1 ] =SND_AUDIOCODEC_AAC ; caps->codecs[2 ] =SND_AUDIOCODEC_FLAC ; + caps->codecs[3 ] =SND_AUDIOCODEC_OPUS_RAW ;return 0 ; }
sound/soc/qcom/qdsp6/q6apm.c +3 -0
@@ -354,6 +354,9 @@ int q6apm_set_real_module_id(struct device *dev, struct q6apm_graph *graph,case SND_AUDIOCODEC_FLAC : module_id =MODULE_ID_FLAC_DEC ;break ; +case SND_AUDIOCODEC_OPUS_RAW : + module_id =MODULE_ID_OPUS_DEC ; +break ;default :return -EINVAL ; }
Signature · 1 line
2.47.3
Re: [PATCH v3 3/3] ASoC: qcom: qdsp6/audioreach: add support for offloading raw opus playback
Quoted 2 lines · Alexey Klimov
Add support for OPUS module, OPUS format ID, media format payload struct and make it all recognizable by audioreach compress playback path.
Quoted 35 lines · Alexey Klimov
This series adds support in kernel to recognise raw (or plain) OPUS codec playback for compress offloading. At this point this series doesn't deal with OPUS packets packed in any kind of containers (OGG or others) and focuses on adding missing bits for pure OPUS packets.
The second patch adds its usage in Qualcomm Audio DSP code. To correctly recognise raw OPUS packets by qdsp6, each packets needs to be prepended with 4-bytes field that contains length of a raw OPUS packet. It is expected to be useful for usecases when OPUS packets are streamed over network and they are not encapsulated in a container. Userspace application that will use the compress API has to manually add such 4-bytes long field to each OPUS packet.
This is tested on sm8750-mtp. It is expected that next hardware revisions will also support raw OPUS codec offloading.
Dependencies are:
-- hardware with DSP that supports decoding OPUS packets (>= Qualcomm sm8750); -- adsp fastrpc;
-- explicitly setting format in sm8750 soundcard driver
https://lore.kernel.org/linux-sound/[email protected]/
-- running adsprpcd tool with support for Audio PD and DSP libraries loading support (or its alternative);
-- tinycompress fcplay tool that will prepare raw opus packets and do the required addition of length field;
-- mfc module in topology compress-playback path, that module is expected to parse channel mapping and do the required things to map streams/channels to physically-present output channels on backend.The userspace tinycompress tool with support for raw OPUS compress playback is located here:
https://github.com/laklimov/tinycompress_opus
branch:opus_v3_workinprogress
Quoted 5 lines
The userspace tool is not expected that it is ready and still needs some work, for instance recognition that it runs on Qualcomm hardware. More like working PoC.
Signed-off-by Alexey Klimov <[email protected]>
Applied now to for-next branch for 6.18.
For further improvements, please submit on top of that.
thanks,
Takashi