ref: 58208f448dd1287ae11ed3030cf24b7046090f49
parent: 82485dd2c4f4dfe8ed0d89eebd4e4f6c0a091532
author: Jean-Marc Valin <jeanmarcv@google.com>
date: Tue Jun 11 22:06:40 EDT 2024
Refactoring analysis downmix to use sig scaling
--- a/celt/arch.h
+++ b/celt/arch.h
@@ -138,7 +138,6 @@
#ifdef ENABLE_RES24
typedef opus_val32 opus_res;
#define RES_SHIFT 8
-#define SCALEIN(a) (a)
#define SIG2RES(a) PSHR32(a, SIG_SHIFT-RES_SHIFT)
#define RES2INT16(a) SAT16(PSHR32(a, RES_SHIFT))
#define RES2FLOAT(a) ((1.f/32768.f/256.)*(a))
@@ -150,7 +149,6 @@
#else
typedef opus_val16 opus_res;
#define RES_SHIFT 0
-#define SCALEIN(a) (a)
#define SIG2RES(a) SIG2WORD16(a)
#define RES2INT16(a) (a)
#define RES2FLOAT(a) ((1.f/32768.f)*(a))
@@ -162,6 +160,8 @@
#endif
#define RES2VAL16(a) RES2INT16(a)
+#define FLOAT2SIG(a) float2int(((opus_int32)32768<<SIG_SHIFT)*(a))
+#define INT16TOSIG(a) SHL32(EXTEND32(a), SIG_SHIFT)
#define celt_isnan(x) 0
@@ -314,8 +314,6 @@
#define DIV32_16(a,b) (((opus_val32)(a))/(opus_val16)(b))
#define DIV32(a,b) (((opus_val32)(a))/(opus_val32)(b))
-#define SCALEIN(a) ((a)*CELT_SIG_SCALE)
-
#define SIG2RES(a) ((1/CELT_SIG_SCALE)*(a))
#define RES2INT16(a) FLOAT2INT16(a)
#define RES2FLOAT(a) (a)
@@ -326,6 +324,8 @@
#define MULT16_RES_Q15(a,b) MULT16_16_Q15(a,b)
#define RES2VAL16(a) (a)
+#define FLOAT2SIG(a) ((a)*CELT_SIG_SCALE)
+#define INT16TOSIG(a) ((float)(a))
#endif /* !FIXED_POINT */
--- a/src/analysis.c
+++ b/src/analysis.c
@@ -163,7 +163,6 @@
static opus_val32 downmix_and_resample(downmix_func downmix, const void *_x, opus_val32 *y, opus_val32 S[3], int subframe, int offset, int c1, int c2, int C, int Fs)
{
VARDECL(opus_val32, tmp);
- opus_val32 scale;
int j;
opus_val32 ret = 0;
SAVE_STACK;
@@ -180,17 +179,11 @@
ALLOC(tmp, subframe, opus_val32);
downmix(_x, tmp, subframe, offset, c1, c2, C);
-#ifdef FIXED_POINT
- scale = (1<<SIG_SHIFT);
-#else
- scale = 1.f/32768;
-#endif
- if (c2==-2)
- scale /= C;
- else if (c2>-1)
- scale /= 2;
- for (j=0;j<subframe;j++)
- tmp[j] *= scale;
+ if ((c2==-2 && C==2) || c2>-1) {
+ for (j=0;j<subframe;j++) {
+ tmp[j] = HALF32(tmp[j]);
+ }
+ }
if (Fs == 48000)
{
ret = silk_resampler_down2_hp(S, y, tmp, subframe);
@@ -211,6 +204,9 @@
silk_resampler_down2_hp(S, y, tmp3x, 3*subframe);
}
RESTORE_STACK;
+#ifndef FIXED_POINT
+ ret *= 1.f/32768/32768;
+#endif
return ret;
}
@@ -422,7 +418,7 @@
#define SCALE_COMPENS (1.f/((opus_int32)1<<(15+SIG_SHIFT)))
#define SCALE_ENER(e) ((SCALE_COMPENS*SCALE_COMPENS)*(e))
#else
-#define SCALE_ENER(e) (e)
+#define SCALE_ENER(e) ((1.f/32768/32768)*e)
#endif
#ifdef FIXED_POINT
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -692,12 +692,6 @@
}
#ifndef DISABLE_FLOAT_API
-#ifdef FIXED_POINT
-#define PCM2VAL(x) FLOAT2INT16(x)
-#else
-#define PCM2VAL(x) SCALEIN(x)
-#endif
-
void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
{
const float *x;
@@ -705,11 +699,11 @@
x = (const float *)_x;
for (j=0;j<subframe;j++)
- y[j] = PCM2VAL(x[(j+offset)*C+c1]);
+ y[j] = FLOAT2SIG(x[(j+offset)*C+c1]);
if (c2>-1)
{
for (j=0;j<subframe;j++)
- y[j] += PCM2VAL(x[(j+offset)*C+c2]);
+ y[j] += FLOAT2SIG(x[(j+offset)*C+c2]);
} else if (c2==-2)
{
int c;
@@ -716,7 +710,7 @@
for (c=1;c<C;c++)
{
for (j=0;j<subframe;j++)
- y[j] += PCM2VAL(x[(j+offset)*C+c]);
+ y[j] += FLOAT2SIG(x[(j+offset)*C+c]);
}
}
}
@@ -729,11 +723,11 @@
x = (const opus_int16 *)_x;
for (j=0;j<subframe;j++)
- y[j] = x[(j+offset)*C+c1];
+ y[j] = INT16TOSIG(x[(j+offset)*C+c1]);
if (c2>-1)
{
for (j=0;j<subframe;j++)
- y[j] += x[(j+offset)*C+c2];
+ y[j] += INT16TOSIG(x[(j+offset)*C+c2]);
} else if (c2==-2)
{
int c;
@@ -740,7 +734,7 @@
for (c=1;c<C;c++)
{
for (j=0;j<subframe;j++)
- y[j] += x[(j+offset)*C+c];
+ y[j] += INT16TOSIG(x[(j+offset)*C+c]);
}
}
}
--
⑨