Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(655582)

Unified Diff: src/opusdec.c

Issue 1358: Dithering improvement
Patch Set: oops Created 2 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/opusdec.c
diff --git a/src/opusdec.c b/src/opusdec.c
index d085f04eacdfd49759ffdb73db805562ba396720..8ee37e9300a3d392825d22d3f8aaa601acc51234 100644
--- a/src/opusdec.c
+++ b/src/opusdec.c
@@ -121,7 +121,6 @@ struct shapestate {
float * b_buf;
float * a_buf;
int fs;
- int mute;
};
static unsigned int rngseed = 22222;
@@ -166,44 +165,53 @@ static void quit(int _x) {
reconstruction filters or downstream resampling in any case.*/
static inline void shape_dither_toshort(shapestate *_ss, short *_o, float *_i, int _n, int _CC)
{
- const float gains[3]={32768.f-15.f,32768.f-15.f,32768.f-3.f};
- const float fcoef[3][8] =
+ const float fcoef[2][8] =
{
{2.2374f, -.7339f, -.1251f, -.6033f, 0.9030f, .0116f, -.5853f, -.2571f}, /* 48.0kHz noise shaping filter sd=2.34*/
{2.2061f, -.4706f, -.2534f, -.6214f, 1.0587f, .0676f, -.6054f, -.2738f}, /* 44.1kHz noise shaping filter sd=2.51*/
- {1.0000f, 0.0000f, 0.0000f, 0.0000f, 0.0000f,0.0000f, 0.0000f, 0.0000f}, /* lowpass noise shaping filter sd=0.65*/
};
+ float coef[8]={0};
int i;
- int rate=_ss->fs==44100?1:(_ss->fs==48000?0:2);
- float gain=gains[rate];
float *b_buf;
float *a_buf;
- int mute=_ss->mute;
+ float fs = _ss->fs;
+ int dither=0;
+ if (fs>46000)
+ memcpy(coef, fcoef[0], 8*sizeof(*coef));
+ else if (fs>43000)
+ memcpy(coef, fcoef[1], 8*sizeof(*coef));
+ else
+ {
+ float radius, theta, alpha;
+ radius = 0.7;
+ theta = 2*M_PI*3000/fs;
+ alpha = .5 - 3000*2/fs;
+ coef[0] = 2*radius*cos(theta) - alpha;
+ coef[1] = 2*alpha*radius*cos(theta) - radius*radius;
+ coef[2] = -alpha*radius*radius;
+ dither = 1;
+ }
b_buf=_ss->b_buf;
a_buf=_ss->a_buf;
- /*In order to avoid replacing digital silence with quiet dither noise
- we mute if the output has been silent for a while*/
- if(mute>64)
- memset(a_buf,0,sizeof(float)*_CC*4);
for(i=0;i<_n;i++)
{
int c;
int pos = i*_CC;
- int silent=1;
for(c=0;c<_CC;c++)
{
int j, si;
float r,s,err=0;
- silent&=_i[pos+c]==0;
- s=_i[pos+c]*gain;
+ s=_i[pos+c];
for(j=0;j<4;j++)
- err += fcoef[rate][j]*b_buf[c*4+j] - fcoef[rate][j+4]*a_buf[c*4+j];
+ err += coef[j]*b_buf[c*4+j] - coef[j+4]*a_buf[c*4+j];
memmove(&a_buf[c*4+1],&a_buf[c*4],sizeof(float)*3);
memmove(&b_buf[c*4+1],&b_buf[c*4],sizeof(float)*3);
a_buf[c*4]=err;
s = s - err;
- r=(float)fast_rand()*(1/(float)UINT_MAX) - (float)fast_rand()*(1/(float)UINT_MAX);
- if (mute>16)r=0;
+ if (dither)
+ r=(float)fast_rand()*(1/(float)UINT_MAX) - .5;
+ else
+ r = 0;
/*Clamp in float out of paranoia that the input will be >96 dBFS and wrap if the
integer is clamped.*/
_o[pos+c] = si = float2int(fmaxf(-32768,fminf(s + r,32767)));
@@ -212,12 +220,9 @@ static inline void shape_dither_toshort(shapestate *_ss, short *_o, float *_i, i
However, small amounts-- at the level which could normally be created by
dither and rounding-- are harmless and can even reduce clipping somewhat
due to the clipping sometimes reducing the dither+rounding error.*/
- b_buf[c*4] = (mute>16)?0:fmaxf(-1.5f,fminf(si - s,1.5f));
+ b_buf[c*4] = fmaxf(-1.5f,fminf(si - s,1.5f));
}
- mute++;
- if(!silent)mute=0;
}
- _ss->mute=MINI(mute,960);
}
static void print_comments(char *comments, int length)
@@ -707,7 +712,6 @@ int main(int argc, char **argv)
output=0;
shapemem.a_buf=0;
shapemem.b_buf=0;
- shapemem.mute=960;
shapemem.fs=0;
/*Process options*/
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld