audioconvert: use 24 bits for 32 formats

The float only preserves 24 bits so use this. Otherwise we get overflows
and errors in clang.
This commit is contained in:
Wim Taymans 2022-07-07 18:38:32 +02:00
parent 43b964ea26
commit b8a4bf880f
4 changed files with 92 additions and 42 deletions

View file

@ -538,8 +538,9 @@ conv_f32d_to_s32_1s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
uint32_t n, unrolled;
__m128 in[1];
__m128i out[4];
__m128 scale = _mm_set1_ps(S32_SCALE);
__m128 int_max = _mm_set1_ps(S32_MAX);
__m128 scale = _mm_set1_ps(S24_SCALE);
__m128 int_max = _mm_set1_ps(S24_MAX);
__m128 int_min = _mm_set1_ps(S24_MIN);
if (SPA_IS_ALIGNED(s0, 16))
unrolled = n_samples & ~3;
@ -549,7 +550,9 @@ conv_f32d_to_s32_1s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
for(n = 0; n < unrolled; n += 4) {
in[0] = _mm_mul_ps(_mm_load_ps(&s0[n]), scale);
in[0] = _mm_min_ps(in[0], int_max);
in[0] = _mm_max_ps(in[0], int_min);
out[0] = _mm_cvttps_epi32(in[0]);
out[0] = _mm_slli_epi32(out[0], 8);
out[1] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(0, 3, 2, 1));
out[2] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(1, 0, 3, 2));
out[3] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(2, 1, 0, 3));
@ -564,7 +567,8 @@ conv_f32d_to_s32_1s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[0] = _mm_load_ss(&s0[n]);
in[0] = _mm_mul_ss(in[0], scale);
in[0] = _mm_min_ss(in[0], int_max);
*d = _mm_cvtss_si32(in[0]);
in[0] = _mm_max_ss(in[0], int_min);
*d = _mm_cvtss_si32(in[0]) << 8;
d += n_channels;
}
}
@ -578,8 +582,9 @@ conv_f32d_to_s32_2s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
uint32_t n, unrolled;
__m256 in[2];
__m256i out[2], t[2];
__m256 scale = _mm256_set1_ps(S32_SCALE);
__m256 int_max = _mm256_set1_ps(S32_MAX);
__m256 scale = _mm256_set1_ps(S24_SCALE);
__m256 int_min = _mm256_set1_ps(S24_MIN);
__m256 int_max = _mm256_set1_ps(S24_MAX);
if (SPA_IS_ALIGNED(s0, 32) &&
SPA_IS_ALIGNED(s1, 32))
@ -593,9 +598,13 @@ conv_f32d_to_s32_2s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[0] = _mm256_min_ps(in[0], int_max);
in[1] = _mm256_min_ps(in[1], int_max);
in[0] = _mm256_max_ps(in[0], int_min);
in[1] = _mm256_max_ps(in[1], int_min);
out[0] = _mm256_cvttps_epi32(in[0]); /* a0 a1 a2 a3 a4 a5 a6 a7 */
out[1] = _mm256_cvttps_epi32(in[1]); /* b0 b1 b2 b3 b4 b5 b6 b7 */
out[0] = _mm256_slli_epi32(out[0], 8);
out[1] = _mm256_slli_epi32(out[1], 8);
t[0] = _mm256_unpacklo_epi32(out[0], out[1]); /* a0 b0 a1 b1 a4 b4 a5 b5 */
t[1] = _mm256_unpackhi_epi32(out[0], out[1]); /* a2 b2 a3 b3 a6 b6 a7 b7 */
@ -624,8 +633,9 @@ conv_f32d_to_s32_2s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
for(; n < n_samples; n++) {
__m128 in[2];
__m128i out[2];
__m128 scale = _mm_set1_ps(S32_SCALE);
__m128 int_max = _mm_set1_ps(S32_MAX);
__m128 scale = _mm_set1_ps(S24_SCALE);
__m128 int_min = _mm_set1_ps(S24_MIN);
__m128 int_max = _mm_set1_ps(S24_MAX);
in[0] = _mm_load_ss(&s0[n]);
in[1] = _mm_load_ss(&s1[n]);
@ -634,7 +644,9 @@ conv_f32d_to_s32_2s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[0] = _mm_mul_ps(in[0], scale);
in[0] = _mm_min_ps(in[0], int_max);
in[0] = _mm_max_ps(in[0], int_min);
out[0] = _mm_cvttps_epi32(in[0]);
out[0] = _mm_slli_epi32(out[0], 8);
_mm_storel_epi64((__m128i*)d, out[0]);
d += n_channels;
}
@ -649,8 +661,9 @@ conv_f32d_to_s32_4s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
uint32_t n, unrolled;
__m256 in[4];
__m256i out[4], t[4];
__m256 scale = _mm256_set1_ps(S32_SCALE);
__m256 int_max = _mm256_set1_ps(S32_MAX);
__m256 scale = _mm256_set1_ps(S24_SCALE);
__m256 int_min = _mm256_set1_ps(S24_MIN);
__m256 int_max = _mm256_set1_ps(S24_MAX);
if (SPA_IS_ALIGNED(s0, 32) &&
SPA_IS_ALIGNED(s1, 32) &&
@ -670,11 +683,19 @@ conv_f32d_to_s32_4s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[1] = _mm256_min_ps(in[1], int_max);
in[2] = _mm256_min_ps(in[2], int_max);
in[3] = _mm256_min_ps(in[3], int_max);
in[0] = _mm256_max_ps(in[0], int_min);
in[1] = _mm256_max_ps(in[1], int_min);
in[2] = _mm256_max_ps(in[2], int_min);
in[3] = _mm256_max_ps(in[3], int_min);
out[0] = _mm256_cvttps_epi32(in[0]); /* a0 a1 a2 a3 a4 a5 a6 a7 */
out[1] = _mm256_cvttps_epi32(in[1]); /* b0 b1 b2 b3 b4 b5 b6 b7 */
out[2] = _mm256_cvttps_epi32(in[2]); /* c0 c1 c2 c3 c4 c5 c6 c7 */
out[3] = _mm256_cvttps_epi32(in[3]); /* d0 d1 d2 d3 d4 d5 d6 d7 */
out[0] = _mm256_slli_epi32(out[0], 8);
out[1] = _mm256_slli_epi32(out[1], 8);
out[2] = _mm256_slli_epi32(out[2], 8);
out[3] = _mm256_slli_epi32(out[3], 8);
t[0] = _mm256_unpacklo_epi32(out[0], out[1]); /* a0 b0 a1 b1 a4 b4 a5 b5 */
t[1] = _mm256_unpackhi_epi32(out[0], out[1]); /* a2 b2 a3 b3 a6 b6 a7 b7 */
@ -699,8 +720,9 @@ conv_f32d_to_s32_4s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
for(; n < n_samples; n++) {
__m128 in[4];
__m128i out[4];
__m128 scale = _mm_set1_ps(S32_SCALE);
__m128 int_max = _mm_set1_ps(S32_MAX);
__m128 scale = _mm_set1_ps(S24_SCALE);
__m128 int_min = _mm_set1_ps(S24_MIN);
__m128 int_max = _mm_set1_ps(S24_MAX);
in[0] = _mm_load_ss(&s0[n]);
in[1] = _mm_load_ss(&s1[n]);
@ -713,7 +735,9 @@ conv_f32d_to_s32_4s_avx2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[0] = _mm_mul_ps(in[0], scale);
in[0] = _mm_min_ps(in[0], int_max);
in[0] = _mm_max_ps(in[0], int_min);
out[0] = _mm_cvttps_epi32(in[0]);
out[0] = _mm_slli_epi32(out[0], 8);
_mm_storeu_si128((__m128i*)d, out[0]);
d += n_channels;
}

View file

@ -338,7 +338,7 @@ conv_s32_to_f32d_1s_sse2(void *data, void * SPA_RESTRICT dst[], const void * SPA
float *d0 = dst[0];
uint32_t n, unrolled;
__m128i in;
__m128 out, factor = _mm_set1_ps(1.0f / S32_SCALE);
__m128 out, factor = _mm_set1_ps(1.0f / S24_SCALE);
if (SPA_IS_ALIGNED(d0, 16))
unrolled = n_samples & ~3;
@ -350,13 +350,14 @@ conv_s32_to_f32d_1s_sse2(void *data, void * SPA_RESTRICT dst[], const void * SPA
s[1*n_channels],
s[2*n_channels],
s[3*n_channels]);
in = _mm_srai_epi32(in, 8);
out = _mm_cvtepi32_ps(in);
out = _mm_mul_ps(out, factor);
_mm_store_ps(&d0[n], out);
s += 4*n_channels;
}
for(; n < n_samples; n++) {
out = _mm_cvtsi32_ss(factor, s[0]);
out = _mm_cvtsi32_ss(factor, s[0]>>8);
out = _mm_mul_ss(out, factor);
_mm_store_ss(&d0[n], out);
s += n_channels;
@ -383,8 +384,9 @@ conv_f32d_to_s32_1s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
uint32_t n, unrolled;
__m128 in[1];
__m128i out[4];
__m128 scale = _mm_set1_ps(S32_SCALE);
__m128 int_max = _mm_set1_ps(S32_MAX);
__m128 scale = _mm_set1_ps(S24_SCALE);
__m128 int_min = _mm_set1_ps(S24_MIN);
__m128 int_max = _mm_set1_ps(S24_MAX);
if (SPA_IS_ALIGNED(s0, 16))
unrolled = n_samples & ~3;
@ -394,7 +396,9 @@ conv_f32d_to_s32_1s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
for(n = 0; n < unrolled; n += 4) {
in[0] = _mm_mul_ps(_mm_load_ps(&s0[n]), scale);
in[0] = _mm_min_ps(in[0], int_max);
in[0] = _mm_max_ps(in[0], int_min);
out[0] = _mm_cvttps_epi32(in[0]);
out[0] = _mm_slli_epi32(out[0], 8);
out[1] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(0, 3, 2, 1));
out[2] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(1, 0, 3, 2));
out[3] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(2, 1, 0, 3));
@ -409,6 +413,7 @@ conv_f32d_to_s32_1s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[0] = _mm_load_ss(&s0[n]);
in[0] = _mm_mul_ss(in[0], scale);
in[0] = _mm_min_ss(in[0], int_max);
in[0] = _mm_max_ss(in[0], int_min);
*d = _mm_cvtss_si32(in[0]);
d += n_channels;
}
@ -423,8 +428,9 @@ conv_f32d_to_s32_2s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
uint32_t n, unrolled;
__m128 in[2];
__m128i out[2], t[2];
__m128 scale = _mm_set1_ps(S32_SCALE);
__m128 int_max = _mm_set1_ps(S32_MAX);
__m128 scale = _mm_set1_ps(S24_SCALE);
__m128 int_min = _mm_set1_ps(S24_MIN);
__m128 int_max = _mm_set1_ps(S24_MAX);
if (SPA_IS_ALIGNED(s0, 16) &&
SPA_IS_ALIGNED(s1, 16))
@ -437,10 +443,14 @@ conv_f32d_to_s32_2s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[1] = _mm_mul_ps(_mm_load_ps(&s1[n]), scale);
in[0] = _mm_min_ps(in[0], int_max);
in[0] = _mm_max_ps(in[0], int_min);
in[1] = _mm_min_ps(in[1], int_max);
in[1] = _mm_max_ps(in[1], int_min);
out[0] = _mm_cvttps_epi32(in[0]);
out[1] = _mm_cvttps_epi32(in[1]);
out[0] = _mm_slli_epi32(out[0], 8);
out[1] = _mm_slli_epi32(out[1], 8);
t[0] = _mm_unpacklo_epi32(out[0], out[1]);
t[1] = _mm_unpackhi_epi32(out[0], out[1]);
@ -460,6 +470,7 @@ conv_f32d_to_s32_2s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[0] = _mm_mul_ps(in[0], scale);
in[0] = _mm_min_ps(in[0], int_max);
out[0] = _mm_cvttps_epi32(in[0]);
out[0] = _mm_slli_epi32(out[0], 8);
_mm_storel_epi64((__m128i*)d, out[0]);
d += n_channels;
}
@ -474,8 +485,9 @@ conv_f32d_to_s32_4s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
uint32_t n, unrolled;
__m128 in[4];
__m128i out[4];
__m128 scale = _mm_set1_ps(S32_SCALE);
__m128 int_max = _mm_set1_ps(S32_MAX);
__m128 scale = _mm_set1_ps(S24_SCALE);
__m128 int_min = _mm_set1_ps(S24_MIN);
__m128 int_max = _mm_set1_ps(S24_MAX);
if (SPA_IS_ALIGNED(s0, 16) &&
SPA_IS_ALIGNED(s1, 16) &&
@ -495,6 +507,10 @@ conv_f32d_to_s32_4s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[1] = _mm_min_ps(in[1], int_max);
in[2] = _mm_min_ps(in[2], int_max);
in[3] = _mm_min_ps(in[3], int_max);
in[0] = _mm_max_ps(in[0], int_min);
in[1] = _mm_max_ps(in[1], int_min);
in[2] = _mm_max_ps(in[2], int_min);
in[3] = _mm_max_ps(in[3], int_min);
_MM_TRANSPOSE4_PS(in[0], in[1], in[2], in[3]);
@ -502,6 +518,10 @@ conv_f32d_to_s32_4s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
out[1] = _mm_cvttps_epi32(in[1]);
out[2] = _mm_cvttps_epi32(in[2]);
out[3] = _mm_cvttps_epi32(in[3]);
out[0] = _mm_slli_epi32(out[0], 8);
out[1] = _mm_slli_epi32(out[1], 8);
out[2] = _mm_slli_epi32(out[2], 8);
out[3] = _mm_slli_epi32(out[3], 8);
_mm_storeu_si128((__m128i*)(d + 0*n_channels), out[0]);
_mm_storeu_si128((__m128i*)(d + 1*n_channels), out[1]);
@ -521,7 +541,9 @@ conv_f32d_to_s32_4s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
in[0] = _mm_mul_ps(in[0], scale);
in[0] = _mm_min_ps(in[0], int_max);
in[0] = _mm_max_ps(in[0], int_min);
out[0] = _mm_cvttps_epi32(in[0]);
out[0] = _mm_slli_epi32(out[0], 8);
_mm_storeu_si128((__m128i*)d, out[0]);
d += n_channels;
}
@ -577,8 +599,9 @@ conv_f32d_to_s32_1s_dither_sse2(struct convert *conv, void * SPA_RESTRICT dst, c
uint32_t n, unrolled;
__m128 in[1];
__m128i out[4];
__m128 scale = _mm_set1_ps(S32_SCALE);
__m128 int_max = _mm_set1_ps(S32_MAX);
__m128 scale = _mm_set1_ps(S24_SCALE);
__m128 int_min = _mm_set1_ps(S24_MIN);
__m128 int_max = _mm_set1_ps(S24_MAX);
if (SPA_IS_ALIGNED(s, 16))
unrolled = n_samples & ~3;
@ -589,7 +612,9 @@ conv_f32d_to_s32_1s_dither_sse2(struct convert *conv, void * SPA_RESTRICT dst, c
in[0] = _mm_mul_ps(_mm_load_ps(&s[n]), scale);
in[0] = _mm_add_ps(in[0], _mm_load_ps(&dither[n]));
in[0] = _mm_min_ps(in[0], int_max);
in[0] = _mm_max_ps(in[0], int_min);
out[0] = _mm_cvttps_epi32(in[0]);
out[0] = _mm_slli_epi32(out[0], 8);
out[1] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(0, 3, 2, 1));
out[2] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(1, 0, 3, 2));
out[3] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(2, 1, 0, 3));
@ -605,7 +630,8 @@ conv_f32d_to_s32_1s_dither_sse2(struct convert *conv, void * SPA_RESTRICT dst, c
in[0] = _mm_mul_ss(in[0], scale);
in[0] = _mm_add_ss(in[0], _mm_load_ss(&dither[n]));
in[0] = _mm_min_ss(in[0], int_max);
*d = _mm_cvtss_si32(in[0]);
in[0] = _mm_max_ss(in[0], int_min);
*d = _mm_cvtss_si32(in[0]) << 8;
d += n_channels;
}
}

View file

@ -91,20 +91,20 @@
#define F32_TO_S24_D(v,d) s32_to_s24(SPA_CLAMP((v) * S24_SCALE + (d), S24_MIN, S24_MAX))
#define U32_MIN 0u
#define U32_MAX 4294967295
#define U32_MAX 4294967295u
#define U32_SCALE 2147483648.f
#define U32_OFFS 2147483648.f
#define U32_TO_F32(v) ((((uint32_t)(v)) * (1.0f / U32_SCALE)) - 1.0f)
#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 U32_TO_F32(v) ((((uint32_t)(v)>>8) * (1.0f / U24_SCALE)) - 1.0f)
#define F32_TO_U32(v) ((uint32_t)SPA_CLAMP((v) * U24_SCALE + U24_OFFS, U24_MIN, U24_MAX) << 8)
#define F32_TO_U32_D(v,d) ((uint32_t)SPA_CLAMP((v) * U24_SCALE + U24_OFFS + (d), U24_MIN, U24_MAX) << 8)
#define S32_MIN -2147483648
#define S32_MAX 2147483520
#define S32_MAX 2147483647
#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 S32_TO_F32(v) (((int32_t)(v)>>8) * (1.0f / S24_SCALE))
#define S32S_TO_F32(v) (((int32_t)bswap_32(v)>>8) * (1.0f / S24_SCALE))
#define F32_TO_S32(v) ((int32_t)SPA_CLAMP((v) * S24_SCALE, S24_MIN, S24_MAX) << 8)
#define F32_TO_S32_D(v,d) ((int32_t)SPA_CLAMP((v) * S24_SCALE + (d), S24_MIN>>1, S24_MAX<<2) << 8)
#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))

View file

@ -54,7 +54,7 @@ static void compare_mem(int i, int j, const void *m1, const void *m2, size_t siz
spa_debug_mem(0, m1, size);
spa_debug_mem(0, m2, size);
}
// spa_assert_se(res == 0);
spa_assert_se(res == 0);
}
static void run_test(const char *name,
@ -268,8 +268,8 @@ static void test_s16_f32(void)
static void test_f32_u32(void)
{
static const float in[] = { 0.0f, 1.0f, -1.0f, 0.5f, -0.5f, 1.1f, -1.1f };
static const uint32_t out[] = { 0x80000000, 0xffffffff, 0x0, 0xc0000000, 0x40000000,
0xffffffff, 0x0 };
static const uint32_t out[] = { 0x80000000, 0xffffff00, 0x0, 0xc0000000, 0x40000000,
0xffffff00, 0x0 };
run_test("test_f32_u32", in, sizeof(in[0]), out, sizeof(out[0]), SPA_N_ELEMENTS(out),
true, true, conv_f32_to_u32_c);
@ -279,8 +279,8 @@ static void test_f32_u32(void)
static void test_u32_f32(void)
{
static const uint32_t in[] = { 0x80000000, 0xffffffff, 0x0, 0xc0000000, 0x40000000 };
static const float out[] = { 0.0f, 1.0f, -1.0f, 0.5f, -0.5f, };
static const uint32_t in[] = { 0x80000000, 0xffffff00, 0x0, 0xc0000000, 0x40000000 };
static const float out[] = { 0.0f, 0.999999880791f, -1.0f, 0.5f, -0.5f, };
run_test("test_u32_f32d", in, sizeof(in[0]), out, sizeof(out[0]), SPA_N_ELEMENTS(out),
true, false, conv_u32_to_f32d_c);
@ -291,8 +291,8 @@ static void test_u32_f32(void)
static void test_f32_s32(void)
{
static const float in[] = { 0.0f, 1.0f, -1.0f, 0.5f, -0.5f, 1.1f, -1.1f };
static const int32_t out[] = { 0, 0x7fffff80, 0x80000000, 0x40000000, 0xc0000000,
0x7fffff80, 0x80000000 };
static const int32_t out[] = { 0, 0x7fffff00, 0x80000000, 0x40000000, 0xc0000000,
0x7fffff00, 0x80000000 };
run_test("test_f32_s32", in, sizeof(in[0]), out, sizeof(out[0]), SPA_N_ELEMENTS(out),
true, true, conv_f32_to_s32_c);
@ -318,8 +318,8 @@ static void test_f32_s32(void)
static void test_s32_f32(void)
{
static const int32_t in[] = { 0, 0x7fffff80, 0x80000000, 0x40000000, 0xc0000000 };
static const float out[] = { 0.0f, 0.999999940395f, -1.0f, 0.5, -0.5, };
static const int32_t in[] = { 0, 0x7fffff00, 0x80000000, 0x40000000, 0xc0000000 };
static const float out[] = { 0.0f, 0.999999880791, -1.0f, 0.5, -0.5, };
run_test("test_s32_f32d", in, sizeof(in[0]), out, sizeof(out[0]), SPA_N_ELEMENTS(out),
true, false, conv_s32_to_f32d_c);
@ -588,7 +588,7 @@ static void test_lossless_s32(void)
for (i = S32_MIN; i < S32_MAX; i+=255) {
float v = S32_TO_F32(i);
int32_t t = F32_TO_S32(v);
spa_assert_se(SPA_ABS(i - t) <= 128);
spa_assert_se(SPA_ABS(i - t) <= 256);
}
}
@ -600,7 +600,7 @@ static void test_lossless_u32(void)
for (i = U32_MIN; i < U32_MAX; i+=255) {
float v = U32_TO_F32(i);
uint32_t t = F32_TO_U32(v);
spa_assert_se(i > t ? (i - t) <= 128 : (t - i) <= 128);
spa_assert_se(i > t ? (i - t) <= 256 : (t - i) <= 256);
}
}