audioconvert: improve format conversion

Make dither noise as a value between -0.5 and 0.5 and add this
to the scaled samples.
For this, we first need to do the scaling and then the CLAMP to
the target depth. This optimizes to the same code but allows us
to avoid under and overflows when we add the dither noise.

Add more dithering methods.

Expose a dither.method property on audioconvert. Disable dither when
the target depth > 16.
This commit is contained in:
Wim Taymans 2022-06-29 14:10:15 +02:00
parent d23b96b033
commit 938f2b123e
6 changed files with 408 additions and 171 deletions

View file

@ -37,72 +37,92 @@
#define FMT_OPS_MAX_ALIGN 32
#define U8_MIN 0
#define U8_MAX 255
#define U8_SCALE 127.5f
#define U8_OFFS 128
#define U8_TO_F32(v) ((((uint8_t)(v)) * (1.0f / U8_OFFS)) - 1.0)
#define F32_TO_U8(v) (uint8_t)((SPA_CLAMP(v, -1.0f, 1.0f) * U8_SCALE) + U8_OFFS)
#define U8_MIN 0u
#define U8_MAX 255u
#define U8_SCALE 127.5f
#define U8_OFFS 128.f
#define U8_TO_F32(v) ((((uint8_t)(v)) * (1.0f / U8_OFFS)) - 1.0)
#define F32_TO_U8(v) (uint8_t)SPA_CLAMP((v) * U8_SCALE + U8_OFFS, U8_MIN, U8_MAX)
#define F32_TO_U8_D(v,d) (uint8_t)SPA_CLAMP((v) * U8_SCALE + U8_OFFS + (d), U8_MIN, U8_MAX)
#define S8_MIN -127
#define S8_MAX 127
#define S8_MAX_F 127.0f
#define S8_SCALE 127.0f
#define S8_TO_F32(v) (((int8_t)(v)) * (1.0f / S8_SCALE))
#define F32_TO_S8(v) (int8_t)(SPA_CLAMP(v, -1.0f, 1.0f) * S8_SCALE)
#define S8_MIN -127
#define S8_MAX 127
#define S8_MAX_F 127.0f
#define S8_SCALE 127.0f
#define S8_TO_F32(v) (((int8_t)(v)) * (1.0f / S8_SCALE))
#define F32_TO_S8(v) (int8_t)SPA_CLAMP((v) * S8_SCALE, S8_MIN, S8_MAX)
#define F32_TO_S8_D(v,d) (int8_t)SPA_CLAMP((v) * S8_SCALE + (d), S8_MIN, S8_MAX)
#define U16_MIN 0
#define U16_MAX 65535
#define U16_SCALE 32767.5f
#define U16_OFFS 32768
#define U16_TO_F32(v) ((((uint16_t)(v)) * (1.0f / U16_OFFS)) - 1.0)
#define U16S_TO_F32(v) (((uint16_t)bswap_16((uint16_t)(v)) * (1.0f / U16_OFFS)) - 1.0)
#define F32_TO_U16(v) (uint16_t)((SPA_CLAMP(v, -1.0f, 1.0f) * U16_SCALE) + U16_OFFS)
#define F32_TO_U16S(v) ((uint16_t)bswap_16((uint16_t)((SPA_CLAMP(v, -1.0f, 1.0f) * U16_SCALE) + U16_OFFS)))
#define U16_MIN 0u
#define U16_MAX 65535u
#define U16_SCALE 32767.5f
#define U16_OFFS 32768.f
#define U16_TO_F32(v) ((((uint16_t)(v)) * (1.0f / U16_OFFS)) - 1.0)
#define U16S_TO_F32(v) (((uint16_t)bswap_16((uint16_t)(v)) * (1.0f / U16_OFFS)) - 1.0)
#define F32_TO_U16(v) (uint16_t)SPA_CLAMP((v) * U16_SCALE + U16_OFFS, U16_MIN, U16_MAX)
#define F32_TO_U16_D(v,d) (uint16_t)SPA_CLAMP((v) * U16_SCALE + U16_OFFS + (d), U16_MIN, U16_MAX)
#define F32_TO_U16S(v) bswap_16(F32_TO_U16(v))
#define F32_TO_U16S_D(v,d) bswap_16(F32_TO_U16_D(v,d))
#define S16_MIN -32767
#define S16_MAX 32767
#define S16_MAX_F 32767.0f
#define S16_SCALE 32767.0f
#define S16_TO_F32(v) (((int16_t)(v)) * (1.0f / S16_SCALE))
#define S16S_TO_F32(v) (((int16_t)bswap_16((uint16_t)v)) * (1.0f / S16_SCALE))
#define F32_TO_S16(v) (int16_t)(SPA_CLAMP(v, -1.0f, 1.0f) * S16_SCALE)
#define F32_TO_S16S(v) ((int16_t)bswap_16((uint16_t)(SPA_CLAMP(v, -1.0f, 1.0f) * S16_SCALE)))
#define S16_MIN -32767
#define S16_MAX 32767
#define S16_MAX_F 32767.0f
#define S16_SCALE 32767.0f
#define S16_TO_F32(v) (((int16_t)(v)) * (1.0f / S16_SCALE))
#define S16S_TO_F32(v) (((int16_t)bswap_16(v)) * (1.0f / S16_SCALE))
#define F32_TO_S16(v) (int16_t)SPA_CLAMP((v) * S16_SCALE, S16_MIN, S16_MAX)
#define F32_TO_S16_D(v,d) (int16_t)SPA_CLAMP((v) * S16_SCALE + (d), S16_MIN, S16_MAX)
#define F32_TO_S16S(v) bswap_16(F32_TO_S16(v))
#define F32_TO_S16S_D(v,d) bswap_16(F32_TO_S16_D(v,d))
#define U24_MIN 0
#define U24_MAX 16777215
#define U24_SCALE 8388607.5f
#define U24_OFFS 8388608
#define U24_TO_F32(v) ((((uint32_t)(v)) * (1.0f / U24_OFFS)) - 1.0)
#define F32_TO_U24(v) (uint32_t)((SPA_CLAMP(v, -1.0f, 1.0f) * U24_SCALE) + U24_OFFS)
#define U24_MIN 0u
#define U24_MAX 16777215u
#define U24_SCALE 8388607.5f
#define U24_OFFS 8388608.f
#define U24_TO_F32(v) ((((uint32_t)(v)) * (1.0f / U24_OFFS)) - 1.0)
#define F32_TO_U24(v) (uint32_t)SPA_CLAMP((v) * U24_SCALE + U24_OFFS, U24_MIN, U24_MAX)
#define F32_TO_U24_D(v,d) (uint32_t)SPA_CLAMP((v) * U24_SCALE + U24_OFFS + (d), U24_MIN, U24_MAX)
#define S24_MIN -8388607
#define S24_MAX 8388607
#define S24_MAX_F 8388607.0f
#define S24_SCALE 8388607.0f
#define S24_TO_F32(v) (((int32_t)(v)) * (1.0f / S24_SCALE))
#define F32_TO_S24(v) (int32_t)(SPA_CLAMP(v, -1.0f, 1.0f) * S24_SCALE)
#define S24_MIN -8388607
#define S24_MAX 8388607
#define S24_MAX_F 8388607.0f
#define S24_SCALE 8388607.0f
#define S24_TO_F32(v) (((int32_t)(v)) * (1.0f / S24_SCALE))
#define F32_TO_S24(v) (int32_t)SPA_CLAMP((v) * S24_SCALE, S24_MIN, S24_MAX)
#define F32_TO_S24_D(v,d) (int32_t)SPA_CLAMP((v) * S24_SCALE + (d), S24_MIN, S24_MAX)
#define U32_TO_F32(v) U24_TO_F32(((uint32_t)(v)) >> 8)
#define F32_TO_U32(v) (F32_TO_U24(v) << 8)
#define U32_MIN 0u
#define U32_MAX 4294967040u
#define U32_SCALE 2147483520.f
#define U32_OFFS 2147483520.f
#define U32_TO_F32(v) ((((uint32_t)(v)) * (1.0f / U32_OFFS)) - 1.0)
#define F32_TO_U32(v) (uint32_t)SPA_CLAMP((v) * U32_SCALE + U32_OFFS, U32_MIN, U32_MAX)
#define F32_TO_U32_D(v,d) (uint32_t)SPA_CLAMP((v) * U32_SCALE + U32_OFFS + (d), U32_MIN, U32_MAX)
#define S32_SCALE 2147483648.0f
#define S32_MIN 2147483520.0f
#define S32_TO_F32(v) S24_TO_F32(((int32_t)(v)) >> 8)
#define S32S_TO_F32(v) S24_TO_F32(((int32_t)bswap_32(v)) >> 8)
#define F32_TO_S32(v) (F32_TO_S24(v) << 8)
#define F32_TO_S32S(v) bswap_32((F32_TO_S24(v) << 8))
#define S32_MIN -2147483520
#define S32_MAX 2147483520
#define S32_MAX_F 2147483520.f
#define S32_SCALE 2147483648.f
#define S32_TO_F32(v) (((int32_t)(v)) * (1.0f / S32_SCALE))
#define S32S_TO_F32(v) (((int32_t)bswap_32(v)) * (1.0f / S32_SCALE))
#define F32_TO_S32(v) (int32_t)SPA_CLAMP((v) * S32_SCALE, S32_MIN, S32_MAX)
#define F32_TO_S32_D(v,d) (int32_t)SPA_CLAMP((v) * S32_SCALE + (d), S32_MIN, S32_MAX)
#define F32_TO_S32S(v) bswap_32(F32_TO_S32(v))
#define F32_TO_S32S_D(v,d) bswap_32(F32_TO_S32_D(v,d))
#define U24_32_TO_F32(v) U32_TO_F32((v)<<8)
#define U24_32S_TO_F32(v) U32_TO_F32(((int32_t)bswap_32(v))<<8)
#define F32_TO_U24_32(v) F32_TO_U24(v)
#define F32_TO_U24_32S(v) bswap_32(F32_TO_U24(v))
#define F32_TO_U24_32_D(v,d) F32_TO_U24_D(v,d)
#define F32_TO_U24_32S_D(v,d) bswap_32(F32_TO_U24_D(v,d))
#define S24_32_TO_F32(v) S32_TO_F32((v)<<8)
#define S24_32S_TO_F32(v) S32_TO_F32(((int32_t)bswap_32(v))<<8)
#define F32_TO_S24_32(v) F32_TO_S24(v)
#define F32_TO_S24_32S(v) bswap_32(F32_TO_S24(v))
#define F32_TO_S24_32_D(v,d) F32_TO_S24_D(v,d)
#define F32_TO_S24_32S_D(v,d) bswap_32(F32_TO_S24_D(v,d))
static inline uint32_t read_u24(const void *src)
{
@ -190,16 +210,15 @@ struct convert {
uint32_t src_fmt;
uint32_t dst_fmt;
uint32_t n_channels;
uint32_t rate;
uint32_t cpu_flags;
const char *func_name;
unsigned int is_passthrough:1;
int32_t bias;
int32_t offset;
uint32_t mask;
float scale;
uint32_t random[16 + FMT_OPS_MAX_ALIGN/4];
int32_t *dither;
float *dither;
uint32_t dither_size;
float ns_data[MAX_NS];
@ -295,13 +314,17 @@ DEFINE_FUNCTION(f64_to_f32d, c);
DEFINE_FUNCTION(f64s_to_f32d, c);
DEFINE_FUNCTION(f64d_to_f32, c);
DEFINE_FUNCTION(f32d_to_u8d, c);
DEFINE_FUNCTION(f32d_to_u8d_dither, c);
DEFINE_FUNCTION(f32_to_u8, c);
DEFINE_FUNCTION(f32_to_u8d, c);
DEFINE_FUNCTION(f32d_to_u8, c);
DEFINE_FUNCTION(f32d_to_u8_dither, c);
DEFINE_FUNCTION(f32d_to_s8d, c);
DEFINE_FUNCTION(f32d_to_s8d_dither, c);
DEFINE_FUNCTION(f32_to_s8, c);
DEFINE_FUNCTION(f32_to_s8d, c);
DEFINE_FUNCTION(f32d_to_s8, c);
DEFINE_FUNCTION(f32d_to_s8_dither, c);
DEFINE_FUNCTION(f32d_to_alaw, c);
DEFINE_FUNCTION(f32d_to_ulaw, c);
DEFINE_FUNCTION(f32_to_u16, c);
@ -375,6 +398,7 @@ DEFINE_FUNCTION(s16_to_f32d, sse2);
DEFINE_FUNCTION(s24_to_f32d, sse2);
DEFINE_FUNCTION(s32_to_f32d, sse2);
DEFINE_FUNCTION(f32d_to_s32, sse2);
DEFINE_FUNCTION(f32d_to_s32_dither, sse2);
DEFINE_FUNCTION(f32_to_s16, sse2);
DEFINE_FUNCTION(f32d_to_s16_2, sse2);
DEFINE_FUNCTION(f32d_to_s16, sse2);