From 7d0a8b68e81def412c4a9c1ef45c519ac3099de1 Mon Sep 17 00:00:00 2001 From: Julian Bouzas Date: Wed, 8 Sep 2021 09:15:21 -0400 Subject: [PATCH] audiomixer: add support for U8, U16, U24, U32, S24_32 and U24_32 formats --- spa/plugins/audiomixer/audiomixer.c | 10 ++- spa/plugins/audiomixer/mix-ops-c.c | 117 ++++++++++++++++++++++++++++ spa/plugins/audiomixer/mix-ops.c | 10 +++ spa/plugins/audiomixer/mix-ops.h | 37 +++++++++ 4 files changed, 173 insertions(+), 1 deletion(-) diff --git a/spa/plugins/audiomixer/audiomixer.c b/spa/plugins/audiomixer/audiomixer.c index 35565e95c..5a3d33f82 100644 --- a/spa/plugins/audiomixer/audiomixer.c +++ b/spa/plugins/audiomixer/audiomixer.c @@ -335,11 +335,17 @@ static int port_enum_formats(void *object, SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat, SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_audio), SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw), - SPA_FORMAT_AUDIO_format, SPA_POD_CHOICE_ENUM_Int(6, + SPA_FORMAT_AUDIO_format, SPA_POD_CHOICE_ENUM_Int(12, SPA_AUDIO_FORMAT_S8, + SPA_AUDIO_FORMAT_U8, SPA_AUDIO_FORMAT_S16, + SPA_AUDIO_FORMAT_U16, SPA_AUDIO_FORMAT_S24, + SPA_AUDIO_FORMAT_U24, SPA_AUDIO_FORMAT_S32, + SPA_AUDIO_FORMAT_U32, + SPA_AUDIO_FORMAT_S24_32, + SPA_AUDIO_FORMAT_U24_32, SPA_AUDIO_FORMAT_F32, SPA_AUDIO_FORMAT_F64), SPA_FORMAT_AUDIO_rate, SPA_POD_CHOICE_RANGE_Int(44100, 1, INT32_MAX), @@ -499,10 +505,12 @@ static int calc_width(struct spa_audio_info *info) case SPA_AUDIO_FORMAT_S16P: case SPA_AUDIO_FORMAT_S16: case SPA_AUDIO_FORMAT_S16_OE: + case SPA_AUDIO_FORMAT_U16: return 2; case SPA_AUDIO_FORMAT_S24P: case SPA_AUDIO_FORMAT_S24: case SPA_AUDIO_FORMAT_S24_OE: + case SPA_AUDIO_FORMAT_U24: return 3; default: return 4; diff --git a/spa/plugins/audiomixer/mix-ops-c.c b/spa/plugins/audiomixer/mix-ops-c.c index 6c7c88e9b..990c9fe68 100644 --- a/spa/plugins/audiomixer/mix-ops-c.c +++ b/spa/plugins/audiomixer/mix-ops-c.c @@ -49,6 +49,25 @@ mix_s8_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT } } +void +mix_u8_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], + uint32_t n_src, uint32_t n_samples) +{ + uint32_t i, n; + uint8_t *d = dst; + + if (n_src == 0) + memset(dst, 0, n_samples * ops->n_channels * sizeof(uint8_t)); + else if (dst != src[0]) + memcpy(dst, src[0], n_samples * ops->n_channels * sizeof(uint8_t)); + + for (i = 1; i < n_src; i++) { + const uint8_t *s = src[i]; + for (n = 0; n < n_samples * ops->n_channels; n++) + d[n] = U8_MIX(d[n], s[n]); + } +} + void mix_s16_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], uint32_t n_src, uint32_t n_samples) @@ -68,6 +87,25 @@ mix_s16_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRIC } } +void +mix_u16_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], + uint32_t n_src, uint32_t n_samples) +{ + uint32_t i, n; + uint16_t *d = dst; + + if (n_src == 0) + memset(dst, 0, n_samples * ops->n_channels * sizeof(uint16_t)); + else if (dst != src[0]) + memcpy(dst, src[0], n_samples * ops->n_channels * sizeof(uint16_t)); + + for (i = 1; i < n_src; i++) { + const uint16_t *s = src[i]; + for (n = 0; n < n_samples * ops->n_channels; n++) + d[n] = U16_MIX(d[n], s[n]); + } +} + void mix_s24_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], uint32_t n_src, uint32_t n_samples) @@ -90,6 +128,28 @@ mix_s24_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRIC } } +void +mix_u24_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], + uint32_t n_src, uint32_t n_samples) +{ + uint32_t i, n; + uint8_t *d = dst; + + if (n_src == 0) + memset(dst, 0, n_samples * ops->n_channels * sizeof(uint8_t) * 3); + else if (dst != src[0]) + memcpy(dst, src[0], n_samples * ops->n_channels * sizeof(uint8_t) * 3); + + for (i = 1; i < n_src; i++) { + const uint8_t *s = src[i]; + for (n = 0; n < n_samples * ops->n_channels; n++) { + write_u24(d, U24_MIX(read_u24(d), read_u24(s))); + d += 3; + s += 3; + } + } +} + void mix_s32_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], uint32_t n_src, uint32_t n_samples) @@ -109,6 +169,63 @@ mix_s32_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRIC } } +void +mix_u32_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], + uint32_t n_src, uint32_t n_samples) +{ + uint32_t i, n; + uint32_t *d = dst; + + if (n_src == 0) + memset(dst, 0, n_samples * ops->n_channels * sizeof(uint32_t)); + else if (dst != src[0]) + memcpy(dst, src[0], n_samples * ops->n_channels * sizeof(uint32_t)); + + for (i = 1; i < n_src; i++) { + const uint32_t *s = src[i]; + for (n = 0; n < n_samples * ops->n_channels; n++) + d[n] = U32_MIX(d[n], s[n]); + } +} + +void +mix_s24_32_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], + uint32_t n_src, uint32_t n_samples) +{ + uint32_t i, n; + int32_t *d = dst; + + if (n_src == 0) + memset(dst, 0, n_samples * ops->n_channels * sizeof(int32_t)); + else if (dst != src[0]) + memcpy(dst, src[0], n_samples * ops->n_channels * sizeof(int32_t)); + + for (i = 1; i < n_src; i++) { + const int32_t *s = src[i]; + for (n = 0; n < n_samples * ops->n_channels; n++) + d[n] = S24_32_MIX(d[n], s[n]); + } +} + +void +mix_u24_32_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], + uint32_t n_src, uint32_t n_samples) +{ + uint32_t i, n; + uint32_t *d = dst; + + if (n_src == 0) + memset(dst, 0, n_samples * ops->n_channels * sizeof(uint32_t)); + else if (dst != src[0]) + memcpy(dst, src[0], n_samples * ops->n_channels * sizeof(uint32_t)); + + for (i = 1; i < n_src; i++) { + const uint32_t *s = src[i]; + for (n = 0; n < n_samples * ops->n_channels; n++) + d[n] = U24_32_MIX(d[n], s[n]); + } +} + void mix_f32_c(struct mix_ops *ops, void * SPA_RESTRICT dst, const void * SPA_RESTRICT src[], uint32_t n_src, uint32_t n_samples) diff --git a/spa/plugins/audiomixer/mix-ops.c b/spa/plugins/audiomixer/mix-ops.c index c5281abaf..f5fc697ec 100644 --- a/spa/plugins/audiomixer/mix-ops.c +++ b/spa/plugins/audiomixer/mix-ops.c @@ -68,18 +68,28 @@ static struct mix_info mix_table[] = /* s8 */ { SPA_AUDIO_FORMAT_S8, 0, 0, 1, mix_s8_c }, { SPA_AUDIO_FORMAT_S8P, 0, 0, 1, mix_s8_c }, + { SPA_AUDIO_FORMAT_U8, 0, 0, 1, mix_u8_c }, + { SPA_AUDIO_FORMAT_U8P, 0, 0, 1, mix_u8_c }, /* s16 */ { SPA_AUDIO_FORMAT_S16, 0, 0, 2, mix_s16_c }, { SPA_AUDIO_FORMAT_S16P, 0, 0, 2, mix_s16_c }, + { SPA_AUDIO_FORMAT_U16, 0, 0, 2, mix_u16_c }, /* s24 */ { SPA_AUDIO_FORMAT_S24, 0, 0, 3, mix_s24_c }, { SPA_AUDIO_FORMAT_S24P, 0, 0, 3, mix_s24_c }, + { SPA_AUDIO_FORMAT_U24, 0, 0, 3, mix_u24_c }, /* s32 */ { SPA_AUDIO_FORMAT_S32, 0, 0, 4, mix_s32_c }, { SPA_AUDIO_FORMAT_S32P, 0, 0, 4, mix_s32_c }, + { SPA_AUDIO_FORMAT_U32, 0, 0, 4, mix_u32_c }, + + /* s24_32 */ + { SPA_AUDIO_FORMAT_S24_32, 0, 0, 4, mix_s24_32_c }, + { SPA_AUDIO_FORMAT_S24_32P, 0, 0, 4, mix_s24_32_c }, + { SPA_AUDIO_FORMAT_U24_32, 0, 0, 4, mix_u24_32_c }, }; #define MATCH_CHAN(a,b) ((a) == 0 || (a) == (b)) diff --git a/spa/plugins/audiomixer/mix-ops.h b/spa/plugins/audiomixer/mix-ops.h index 000f13392..3571af690 100644 --- a/spa/plugins/audiomixer/mix-ops.h +++ b/spa/plugins/audiomixer/mix-ops.h @@ -24,6 +24,16 @@ #include +static inline uint32_t read_u24(const void *src) +{ + const uint8_t *s = src; +#if __BYTE_ORDER == __LITTLE_ENDIAN + return (((uint32_t)s[2] << 16) | ((uint32_t)(uint8_t)s[1] << 8) | (uint32_t)(uint8_t)s[0]); +#else + return (((uint32_t)s[0] << 16) | ((uint32_t)(uint8_t)s[1] << 8) | (uint32_t)(uint8_t)s[2]); +#endif +} + static inline int32_t read_s24(const void *src) { const int8_t *s = src; @@ -34,6 +44,20 @@ static inline int32_t read_s24(const void *src) #endif } +static inline void write_u24(void *dst, uint32_t val) +{ + uint8_t *d = dst; +#if __BYTE_ORDER == __LITTLE_ENDIAN + d[0] = (uint8_t) (val); + d[1] = (uint8_t) (val >> 8); + d[2] = (uint8_t) (val >> 16); +#else + d[0] = (uint8_t) (val >> 16); + d[1] = (uint8_t) (val >> 8); + d[2] = (uint8_t) (val); +#endif +} + static inline void write_s24(void *dst, int32_t val) { uint8_t *d = dst; @@ -51,18 +75,25 @@ static inline void write_s24(void *dst, int32_t val) #define S8_MIN -127 #define S8_MAX 127 #define S8_MIX(a, b) (int8_t)(SPA_CLAMP((int16_t)(a) + (int16_t)(b), S8_MIN, S8_MAX)) +#define U8_MIX(a, b) (uint8_t)((int16_t)S8_MIX((int16_t)(a) - S8_MAX, (int16_t)(b) - S8_MAX) + S8_MAX) #define S16_MIN -32767 #define S16_MAX 32767 #define S16_MIX(a, b) (int16_t)(SPA_CLAMP((int32_t)(a) + (int32_t)(b), S16_MIN, S16_MAX)) +#define U16_MIX(a, b) (uint16_t)((int32_t)S16_MIX((int32_t)(a) - S16_MAX, (int32_t)(b) - S16_MAX) + S16_MAX) #define S24_MIN -8388607 #define S24_MAX 8388607 #define S24_MIX(a, b) (int32_t)(SPA_CLAMP((int32_t)(a) + (int32_t)(b), S24_MIN, S24_MAX)) +#define U24_MIX(a, b) (uint32_t)((int32_t)S24_MIX((int32_t)(a) - S24_MAX, (int32_t)(b) - S24_MAX) + S24_MAX) #define S32_MIN -2147483647 #define S32_MAX 2147483647 #define S32_MIX(a, b) (int32_t)(SPA_CLAMP((int64_t)(a) + (int64_t)(b), S32_MIN, S32_MAX)) +#define U32_MIX(a, b) (uint32_t)((int64_t)S32_MIX((int64_t)(a) - S32_MAX, (int64_t)(b) - S32_MAX) + S32_MAX) + +#define S24_32_MIX(a, b) S24_MIX (a, b) +#define U24_32_MIX(a, b) U24_MIX (a, b) #define F32_MIX(a, b) (float)((float)(a) + (float)(b)) @@ -95,9 +126,15 @@ void mix_##name##_##arch(struct mix_ops *ops, void * SPA_RESTRICT dst, \ uint32_t n_samples) \ DEFINE_FUNCTION(s8, c); +DEFINE_FUNCTION(u8, c); DEFINE_FUNCTION(s16, c); +DEFINE_FUNCTION(u16, c); DEFINE_FUNCTION(s24, c); +DEFINE_FUNCTION(u24, c); DEFINE_FUNCTION(s32, c); +DEFINE_FUNCTION(u32, c); +DEFINE_FUNCTION(s24_32, c); +DEFINE_FUNCTION(u24_32, c); DEFINE_FUNCTION(f32, c); DEFINE_FUNCTION(f64, c);