Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
opusdsp: adjust and optimize C function to match assembly
The C and asm versions behaved differently _outside_ of the codec.

The C version returned pre-multiplied 'state' for the next execution
to use right away, while the assembly version outputted non-multiplied
'state' for the next execution to multiply to save instructions.
Since the initial state when initialized or seeking is always 0,
and since C and asm versions were never mixed, there was no issue.

However, comparing outputs directly in checkasm doesn't work without
dividing the initial state by CELT_EMPH_COEFF and multiplying the
returned state by CELT_EMPH_COEFF for the assembly function.

Since its actually faster to do this in C as well, copy the behavior the
asm versions use. As a reminder, the initial state 0 is divided by
CELT_EMPH_COEFF on seek and init (just in case in the future this is
changed, its technically more correct to init with CELT_EMPH_COEFF than 0,
however when seeking this will result in more audiable pops, unlike with 0
where the output gets in sync over a few samples).
  • Loading branch information
cyanreg committed Sep 11, 2019
1 parent 40a433e commit 6b22e28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
6 changes: 5 additions & 1 deletion libavcodec/opus_celt.c
Expand Up @@ -507,7 +507,11 @@ void ff_celt_flush(CeltFrame *f)
memset(block->pf_gains_old, 0, sizeof(block->pf_gains_old));
memset(block->pf_gains_new, 0, sizeof(block->pf_gains_new));

block->emph_coeff = 0.0;
/* libopus uses CELT_EMPH_COEFF on init, but 0 is better since there's
* a lesser discontinuity when seeking.
* The deemphasis functions differ from libopus in that they require
* an initial state divided by the coefficient. */
block->emph_coeff = 0.0f / CELT_EMPH_COEFF;
}
f->seed = 0;

Expand Down
11 changes: 3 additions & 8 deletions libavcodec/opusdsp.c
Expand Up @@ -43,15 +43,10 @@ static void postfilter_c(float *data, int period, float *gains, int len)

static float deemphasis_c(float *y, float *x, float coeff, int len)
{
float state = coeff;
for (int i = 0; i < len; i++)
coeff = y[i] = x[i] + coeff*CELT_EMPH_COEFF;

for (int i = 0; i < len; i++) {
const float tmp = x[i] + state;
state = tmp * CELT_EMPH_COEFF;
y[i] = tmp;
}

return state;
return coeff;
}

av_cold void ff_opus_dsp_init(OpusDSP *ctx)
Expand Down

0 comments on commit 6b22e28

Please sign in to comment.