[PATCH v2 1/2] ALSA: compress: add raw opus codec define and opus decoder structs

Alexey Klimov posted 2 patches 3 weeks, 1 day ago
There is a newer version of this series
[PATCH v2 1/2] ALSA: compress: add raw opus codec define and opus decoder structs
Posted by Alexey Klimov 3 weeks, 1 day ago
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 <srini@kernel.org>
Cc: Vinod Koul <vkoul@kernel.org>
Co-developed-by: Annemarie Porter <annemari@quicinc.com>
Signed-off-by: Annemarie Porter <annemari@quicinc.com>
Signed-off-by: Alexey Klimov <alexey.klimov@linaro.org>
---
 include/uapi/sound/compress_params.h | 49 +++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/include/uapi/sound/compress_params.h b/include/uapi/sound/compress_params.h
index bc7648a30746f4632ecf6695868e79550a431dfa..b3ddd7919f42048b307564d8a6a83e4c8ad2c7fd 100644
--- a/include/uapi/sound/compress_params.h
+++ b/include/uapi/sound/compress_params.h
@@ -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,51 @@ 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 {
+	union {
+		struct {
+			__u8 minor:4;
+			__u8 major:4;
+		} __attribute__((packed)) fields;
+		__u8 version_byte;
+	} 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 +380,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;

-- 
2.47.2
Re: [PATCH v2 1/2] ALSA: compress: add raw opus codec define and opus decoder structs
Posted by Takashi Iwai 3 weeks, 1 day ago
On Wed, 10 Sep 2025 10:11:41 +0200,
Alexey Klimov wrote:
> +struct snd_dec_opus {
> +	union {
> +		struct {
> +			__u8 minor:4;
> +			__u8 major:4;
> +		} __attribute__((packed)) fields;

Bit fields aren't really good for ABI definition, as it's not well
defined.  I'd rather leave it as a u8.  If any, you can provide a
bitmask definition or a macro to retrieve the version numbers.

Also, don't forget to bump the API protocol number.
It's been already increased to 0.4.0 for 64bit tstamp support, and
yours need to increase one more.

(That is, please make sure to create patches based on linux-next, or
 for-next branch of sound git tree.)


thanks,

Takashi
Re: [PATCH v2 1/2] ALSA: compress: add raw opus codec define and opus decoder structs
Posted by Alexey Klimov 2 weeks, 2 days ago
Hi Takashi,

On Wed Sep 10, 2025 at 10:17 AM BST, Takashi Iwai wrote:
> On Wed, 10 Sep 2025 10:11:41 +0200,
> Alexey Klimov wrote:
>> +struct snd_dec_opus {
>> +	union {
>> +		struct {
>> +			__u8 minor:4;
>> +			__u8 major:4;
>> +		} __attribute__((packed)) fields;
>
> Bit fields aren't really good for ABI definition, as it's not well
> defined.  I'd rather leave it as a u8.  If any, you can provide a
> bitmask definition or a macro to retrieve the version numbers.

Okay.

> Also, don't forget to bump the API protocol number.
> It's been already increased to 0.4.0 for 64bit tstamp support, and
> yours need to increase one more.

Can you please advice if minor version should be increased i.e. it
should become 0.4.1? Or should it 0.5.0?

> (That is, please make sure to create patches based on linux-next, or
>  for-next branch of sound git tree.)

Previous and this one were (re)based on linux-next at the time of
sending. I didn't really verify against for-next branch of sound git
tree (I know I should).

Thanks,
Alexey
Re: [PATCH v2 1/2] ALSA: compress: add raw opus codec define and opus decoder structs
Posted by Takashi Iwai 2 weeks, 2 days ago
On Tue, 16 Sep 2025 06:20:31 +0200,
Alexey Klimov wrote:
> 
> Hi Takashi,
> 
> On Wed Sep 10, 2025 at 10:17 AM BST, Takashi Iwai wrote:
> > On Wed, 10 Sep 2025 10:11:41 +0200,
> > Alexey Klimov wrote:
> >> +struct snd_dec_opus {
> >> +	union {
> >> +		struct {
> >> +			__u8 minor:4;
> >> +			__u8 major:4;
> >> +		} __attribute__((packed)) fields;
> >
> > Bit fields aren't really good for ABI definition, as it's not well
> > defined.  I'd rather leave it as a u8.  If any, you can provide a
> > bitmask definition or a macro to retrieve the version numbers.
> 
> Okay.
> 
> > Also, don't forget to bump the API protocol number.
> > It's been already increased to 0.4.0 for 64bit tstamp support, and
> > yours need to increase one more.
> 
> Can you please advice if minor version should be increased i.e. it
> should become 0.4.1? Or should it 0.5.0?

0.4.1 should suffice in this case, I believe.

> > (That is, please make sure to create patches based on linux-next, or
> >  for-next branch of sound git tree.)
> 
> Previous and this one were (re)based on linux-next at the time of
> sending. I didn't really verify against for-next branch of sound git
> tree (I know I should).

linux-next should be fine.  The compress-offload 64bit timestamp
change was merged recently, so you might have missed it when you
created the patch.


thanks,

Takashi