Skip to content

Commit

Permalink
Refactoring analysis downmix to use sig scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
jmvalin committed Nov 2, 2024
1 parent 69adba3 commit 5daf8d5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
8 changes: 4 additions & 4 deletions celt/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ typedef opus_val32 celt_ener;
#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))
Expand All @@ -150,7 +149,6 @@ typedef opus_val32 opus_res;
#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))
Expand All @@ -162,6 +160,8 @@ typedef opus_val16 opus_res;
#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

Expand Down Expand Up @@ -314,8 +314,6 @@ static OPUS_INLINE int celt_isnan(float x)
#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)
Expand All @@ -326,6 +324,8 @@ static OPUS_INLINE int celt_isnan(float x)
#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 */
Expand Down
19 changes: 6 additions & 13 deletions src/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ static opus_val32 silk_resampler_down2_hp(
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;
Expand All @@ -180,17 +179,11 @@ static opus_val32 downmix_and_resample(downmix_func downmix, const void *_x, opu
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);
Expand Down Expand Up @@ -422,7 +415,7 @@ static const float std_feature_bias[9] = {
#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
Expand Down
18 changes: 6 additions & 12 deletions src/opus_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,31 +692,25 @@ static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int m
}

#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;
int j;

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;
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]);
}
}
}
Expand All @@ -729,18 +723,18 @@ void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1

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;
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]);
}
}
}
Expand Down

0 comments on commit 5daf8d5

Please sign in to comment.