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

Delta Between Two Patch Sets: src/opusdec.c

Issue 1358: Dithering improvement
Left Patch Set: Created 3 years, 3 months ago
Right Patch Set: oops Created 2 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* Copyright (c) 2002-2007 Jean-Marc Valin 1 /* Copyright (c) 2002-2007 Jean-Marc Valin
2 Copyright (c) 2008 CSIRO 2 Copyright (c) 2008 CSIRO
3 Copyright (c) 2007-2013 Xiph.Org Foundation 3 Copyright (c) 2007-2013 Xiph.Org Foundation
4 File: opusdec.c 4 File: opusdec.c
5 5
6 Redistribution and use in source and binary forms, with or without 6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions 7 modification, are permitted provided that the following conditions
8 are met: 8 are met:
9 9
10 - Redistributions of source code must retain the above copyright 10 - Redistributions of source code must retain the above copyright
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 by the peak error of 1.5 +20 dB though this much is unobservable rare) 158 by the peak error of 1.5 +20 dB though this much is unobservable rare)
159 so to avoid clipping the signal is attenuated by a couple thousandths 159 so to avoid clipping the signal is attenuated by a couple thousandths
160 of a dB. Initially the approach taken here was to only attenuate by 160 of a dB. Initially the approach taken here was to only attenuate by
161 the 99.9th percentile, making clipping rare but not impossible (like 161 the 99.9th percentile, making clipping rare but not impossible (like
162 SoX) but the limited gain of the filter means that the worst case was 162 SoX) but the limited gain of the filter means that the worst case was
163 only two thousandths of a dB more, so this just uses the worst case. 163 only two thousandths of a dB more, so this just uses the worst case.
164 The attenuation is probably also helpful to prevent clipping in the DAC 164 The attenuation is probably also helpful to prevent clipping in the DAC
165 reconstruction filters or downstream resampling in any case.*/ 165 reconstruction filters or downstream resampling in any case.*/
166 static inline void shape_dither_toshort(shapestate *_ss, short *_o, float *_i, i nt _n, int _CC) 166 static inline void shape_dither_toshort(shapestate *_ss, short *_o, float *_i, i nt _n, int _CC)
167 { 167 {
168 const float gains[3]={32768.f-15.f,32768.f-15.f,32768.f-3.f}; 168 const float fcoef[2][8] =
169 const float fcoef[3][8] =
170 { 169 {
171 {2.2374f, -.7339f, -.1251f, -.6033f, 0.9030f, .0116f, -.5853f, -.2571f}, /* 48.0kHz noise shaping filter sd=2.34*/ 170 {2.2374f, -.7339f, -.1251f, -.6033f, 0.9030f, .0116f, -.5853f, -.2571f}, /* 48.0kHz noise shaping filter sd=2.34*/
172 {2.2061f, -.4706f, -.2534f, -.6214f, 1.0587f, .0676f, -.6054f, -.2738f}, /* 44.1kHz noise shaping filter sd=2.51*/ 171 {2.2061f, -.4706f, -.2534f, -.6214f, 1.0587f, .0676f, -.6054f, -.2738f}, /* 44.1kHz noise shaping filter sd=2.51*/
173 {1.0000f, 0.0000f, 0.0000f, 0.0000f, 0.0000f,0.0000f, 0.0000f, 0.0000f}, /* lowpass noise shaping filter sd=0.65*/
174 }; 172 };
173 float coef[8]={0};
175 int i; 174 int i;
176 int rate=_ss->fs==44100?1:(_ss->fs==48000?0:2);
177 float gain=gains[rate];
178 float *b_buf; 175 float *b_buf;
179 float *a_buf; 176 float *a_buf;
177 float fs = _ss->fs;
178 int dither=0;
179 if (fs>46000)
180 memcpy(coef, fcoef[0], 8*sizeof(*coef));
181 else if (fs>43000)
182 memcpy(coef, fcoef[1], 8*sizeof(*coef));
183 else
184 {
185 float radius, theta, alpha;
186 radius = 0.7;
187 theta = 2*M_PI*3000/fs;
188 alpha = .5 - 3000*2/fs;
189 coef[0] = 2*radius*cos(theta) - alpha;
190 coef[1] = 2*alpha*radius*cos(theta) - radius*radius;
191 coef[2] = -alpha*radius*radius;
192 dither = 1;
193 }
180 b_buf=_ss->b_buf; 194 b_buf=_ss->b_buf;
181 a_buf=_ss->a_buf; 195 a_buf=_ss->a_buf;
182 for(i=0;i<_n;i++) 196 for(i=0;i<_n;i++)
183 { 197 {
184 int c; 198 int c;
185 int pos = i*_CC; 199 int pos = i*_CC;
186 for(c=0;c<_CC;c++) 200 for(c=0;c<_CC;c++)
187 { 201 {
188 int j, si; 202 int j, si;
189 float r,s,err=0; 203 float r,s,err=0;
190 s=_i[pos+c]*gain; 204 s=_i[pos+c];
191 for(j=0;j<4;j++) 205 for(j=0;j<4;j++)
192 err += fcoef[rate][j]*b_buf[c*4+j] - fcoef[rate][j+4]*a_buf[c*4+j]; 206 err += coef[j]*b_buf[c*4+j] - coef[j+4]*a_buf[c*4+j];
193 memmove(&a_buf[c*4+1],&a_buf[c*4],sizeof(float)*3); 207 memmove(&a_buf[c*4+1],&a_buf[c*4],sizeof(float)*3);
194 memmove(&b_buf[c*4+1],&b_buf[c*4],sizeof(float)*3); 208 memmove(&b_buf[c*4+1],&b_buf[c*4],sizeof(float)*3);
195 a_buf[c*4]=err; 209 a_buf[c*4]=err;
196 s = s - err; 210 s = s - err;
197 /* Adds just enough noise to cause signals above -128 dBFS to dither. Sign als below 211 if (dither)
oz 2016/05/09 05:51:38 Should Opus comments and code wrap at 79 chars, fo
198 -132 dBFS will go to zero. Signals in between may turn on and off, but it's low 212 r=(float)fast_rand()*(1/(float)UINT_MAX) - .5;
199 enough that it shouldn't matter. */ 213 else
200 r=(float)fast_rand()*(0.8/(float)UINT_MAX)-.4; 214 r = 0;
201 /*Clamp in float out of paranoia that the input will be >96 dBFS and wrap if the 215 /*Clamp in float out of paranoia that the input will be >96 dBFS and wrap if the
202 integer is clamped.*/ 216 integer is clamped.*/
203 _o[pos+c] = si = float2int(fmaxf(-32768,fminf(s + r,32767))); 217 _o[pos+c] = si = float2int(fmaxf(-32768,fminf(s + r,32767)));
204 /*Including clipping in the noise shaping is generally disastrous: 218 /*Including clipping in the noise shaping is generally disastrous:
205 the futile effort to restore the clipped energy results in more clipping . 219 the futile effort to restore the clipped energy results in more clipping .
206 However, small amounts-- at the level which could normally be created by 220 However, small amounts-- at the level which could normally be created by
207 dither and rounding-- are harmless and can even reduce clipping somewhat 221 dither and rounding-- are harmless and can even reduce clipping somewhat
208 due to the clipping sometimes reducing the dither+rounding error.*/ 222 due to the clipping sometimes reducing the dither+rounding error.*/
209 b_buf[c*4] = fmaxf(-1.5f,fminf(si - s,1.5f)); 223 b_buf[c*4] = fmaxf(-1.5f,fminf(si - s,1.5f));
210 } 224 }
(...skipping 925 matching lines...) Expand 10 before | Expand all | Expand 10 after
1136 if (fout != NULL) 1150 if (fout != NULL)
1137 fclose(fout); 1151 fclose(fout);
1138 1152
1139 #ifdef WIN_UNICODE 1153 #ifdef WIN_UNICODE
1140 free_commandline_arguments_utf8(&argc_utf8, &argv_utf8); 1154 free_commandline_arguments_utf8(&argc_utf8, &argv_utf8);
1141 uninit_console_utf8(); 1155 uninit_console_utf8();
1142 #endif 1156 #endif
1143 1157
1144 return 0; 1158 return 0;
1145 } 1159 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld