Debian Codegraph @

libspeex/vorbis_psy.c

raw
1 /* Copyright (C) 2005 Jean-Marc Valin, CSIRO, Christopher Montgomery
2 File: vorbis_psy.c
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifdef VORBIS_PSYCHO
37
38 #include "arch.h"
39 #include "smallft.h"
40 #include "lpc.h"
41 #include "vorbis_psy.h"
42 #include "os_support.h"
43
44 #include <stdlib.h>
45 #include <math.h>
46 #include <string.h>
47
48 /* psychoacoustic setup ********************************************/
49
50 static VorbisPsyInfo example_tuning = {
51
52 .5,.5,
53 3,3,25,
54
55 /*63 125 250 500 1k 2k 4k 8k 16k*/
56 // vorbis mode 4 style
57 //{-32,-32,-32,-32,-28,-24,-22,-20,-20, -20, -20, -8, -6, -6, -6, -6, -6},
58 { -4, -6, -6, -6, -6, -6, -6, -6, -8, -8,-10,-10, -8, -6, -4, -4, -2},
59
60 {
61 0, 1, 2, 3, 4, 5, 5, 5, /* 7dB */
62 6, 6, 6, 5, 4, 4, 4, 4, /* 15dB */
63 4, 4, 5, 5, 5, 6, 6, 6, /* 23dB */
64 7, 7, 7, 8, 8, 8, 9, 10, /* 31dB */
65 11,12,13,14,15,16,17, 18, /* 39dB */
66 }
67
68 };
69
70
71
72 /* there was no great place to put this.... */
73 #include <stdio.h>
74 static void _analysis_output(char *base,int i,float *v,int n,int bark,int dB){
75 int j;
76 FILE *of;
77 char buffer[80];
78
79 sprintf(buffer,"%s_%d.m",base,i);
80 of=fopen(buffer,"w");
81
82 if(!of)perror("failed to open data dump file");
83
84 for(j=0;j<n;j++){
85 if(bark){
86 float b=toBARK((4000.f*j/n)+.25);
87 fprintf(of,"%f ",b);
88 }else
89 fprintf(of,"%f ",(double)j);
90
91 if(dB){
92 float val;
93 if(v[j]==0.)
94 val=-140.;
95 else
96 val=todB(v[j]);
97 fprintf(of,"%f\n",val);
98 }else{
99 fprintf(of,"%f\n",v[j]);
100 }
101 }
102 fclose(of);
103 }
104
105 static void bark_noise_hybridmp(int n,const long *b,
106 const float *f,
107 float *noise,
108 const float offset,
109 const int fixed){
110
111 float *N=alloca(n*sizeof(*N));
112 float *X=alloca(n*sizeof(*N));
113 float *XX=alloca(n*sizeof(*N));
114 float *Y=alloca(n*sizeof(*N));
115 float *XY=alloca(n*sizeof(*N));
116
117 float tN, tX, tXX, tY, tXY;
118 int i;
119
120 int lo, hi;
121 float R, A, B, D;
122 float w, x, y;
123
124 tN = tX = tXX = tY = tXY = 0.f;
125
126 y = f[0] + offset;
127 if (y < 1.f) y = 1.f;
128
129 w = y * y * .5;
130
131 tN += w;
132 tX += w;
133 tY += w * y;
134
135 N[0] = tN;
136 X[0] = tX;
137 XX[0] = tXX;
138 Y[0] = tY;
139 XY[0] = tXY;
140
141 for (i = 1, x = 1.f; i < n; i++, x += 1.f) {
142
143 y = f[i] + offset;
144 if (y < 1.f) y = 1.f;
145
146 w = y * y;
147
148 tN += w;
149 tX += w * x;
150 tXX += w * x * x;
151 tY += w * y;
152 tXY += w * x * y;
153
154 N[i] = tN;
155 X[i] = tX;
156 XX[i] = tXX;
157 Y[i] = tY;
158 XY[i] = tXY;
159 }
160
161 for (i = 0, x = 0.f;; i++, x += 1.f) {
162
163 lo = b[i] >> 16;
164 if( lo>=0 ) break;
165 hi = b[i] & 0xffff;
166
167 tN = N[hi] + N[-lo];
168 tX = X[hi] - X[-lo];
169 tXX = XX[hi] + XX[-lo];
170 tY = Y[hi] + Y[-lo];
171 tXY = XY[hi] - XY[-lo];
172
173 A = tY * tXX - tX * tXY;
174 B = tN * tXY - tX * tY;
175 D = tN * tXX - tX * tX;
176 R = (A + x * B) / D;
177 if (R < 0.f)
178 R = 0.f;
179
180 noise[i] = R - offset;
181 }
182
183 for ( ;; i++, x += 1.f) {
184
185 lo = b[i] >> 16;
186 hi = b[i] & 0xffff;
187 if(hi>=n)break;
188
189 tN = N[hi] - N[lo];
190 tX = X[hi] - X[lo];
191 tXX = XX[hi] - XX[lo];
192 tY = Y[hi] - Y[lo];
193 tXY = XY[hi] - XY[lo];
194
195 A = tY * tXX - tX * tXY;
196 B = tN * tXY - tX * tY;
197 D = tN * tXX - tX * tX;
198 R = (A + x * B) / D;
199 if (R < 0.f) R = 0.f;
200
201 noise[i] = R - offset;
202 }
203 for ( ; i < n; i++, x += 1.f) {
204
205 R = (A + x * B) / D;
206 if (R < 0.f) R = 0.f;
207
208 noise[i] = R - offset;
209 }
210
211 if (fixed <= 0) return;
212
213 for (i = 0, x = 0.f;; i++, x += 1.f) {
214 hi = i + fixed / 2;
215 lo = hi - fixed;
216 if(lo>=0)break;
217
218 tN = N[hi] + N[-lo];
219 tX = X[hi] - X[-lo];
220 tXX = XX[hi] + XX[-lo];
221 tY = Y[hi] + Y[-lo];
222 tXY = XY[hi] - XY[-lo];
223
224
225 A = tY * tXX - tX * tXY;
226 B = tN * tXY - tX * tY;
227 D = tN * tXX - tX * tX;
228 R = (A + x * B) / D;
229
230 if (R - offset < noise[i]) noise[i] = R - offset;
231 }
232 for ( ;; i++, x += 1.f) {
233
234 hi = i + fixed / 2;
235 lo = hi - fixed;
236 if(hi>=n)break;
237
238 tN = N[hi] - N[lo];
239 tX = X[hi] - X[lo];
240 tXX = XX[hi] - XX[lo];
241 tY = Y[hi] - Y[lo];
242 tXY = XY[hi] - XY[lo];
243
244 A = tY * tXX - tX * tXY;
245 B = tN * tXY - tX * tY;
246 D = tN * tXX - tX * tX;
247 R = (A + x * B) / D;
248
249 if (R - offset < noise[i]) noise[i] = R - offset;
250 }
251 for ( ; i < n; i++, x += 1.f) {
252 R = (A + x * B) / D;
253 if (R - offset < noise[i]) noise[i] = R - offset;
254 }
255 }
256
257 static void _vp_noisemask(VorbisPsy *p,
258 float *logfreq,
259 float *logmask){
260
261 int i,n=p->n/2;
262 float *work=alloca(n*sizeof(*work));
263
264 bark_noise_hybridmp(n,p->bark,logfreq,logmask,
265 140.,-1);
266
267 for(i=0;i<n;i++)work[i]=logfreq[i]-logmask[i];
268
269 bark_noise_hybridmp(n,p->bark,work,logmask,0.,
270 p->vi->noisewindowfixed);
271
272 for(i=0;i<n;i++)work[i]=logfreq[i]-work[i];
273
274 {
275 static int seq=0;
276
277 float work2[n];
278 for(i=0;i<n;i++){
279 work2[i]=logmask[i]+work[i];
280 }
281
282 //_analysis_output("logfreq",seq,logfreq,n,0,0);
283 //_analysis_output("median",seq,work,n,0,0);
284 //_analysis_output("envelope",seq,work2,n,0,0);
285 seq++;
286 }
287
288 for(i=0;i<n;i++){
289 int dB=logmask[i]+.5;
290 if(dB>=NOISE_COMPAND_LEVELS)dB=NOISE_COMPAND_LEVELS-1;
291 if(dB<0)dB=0;
292 logmask[i]= work[i]+p->vi->noisecompand[dB]+p->noiseoffset[i];
293 }
294
295 }
296
297 VorbisPsy *vorbis_psy_init(int rate, int n)
298 {
299 long i,j,lo=-99,hi=1;
300 VorbisPsy *p = speex_alloc(sizeof(VorbisPsy));
301 memset(p,0,sizeof(*p));
302
303 p->n = n;
304 spx_drft_init(&p->lookup, n);
305 p->bark = speex_alloc(n*sizeof(*p->bark));
306 p->rate=rate;
307 p->vi = &example_tuning;
308
309 /* BH4 window */
310 p->window = speex_alloc(sizeof(*p->window)*n);
311 float a0 = .35875f;
312 float a1 = .48829f;
313 float a2 = .14128f;
314 float a3 = .01168f;
315 for(i=0;i<n;i++)
316 p->window[i] = //a0 - a1*cos(2.*M_PI/n*(i+.5)) + a2*cos(4.*M_PI/n*(i+.5)) - a3*cos(6.*M_PI/n*(i+.5));
317 sin((i+.5)/n * M_PI)*sin((i+.5)/n * M_PI);
318 /* bark scale lookups */
319 for(i=0;i<n;i++){
320 float bark=toBARK(rate/(2*n)*i);
321
322 for(;lo+p->vi->noisewindowlomin<i &&
323 toBARK(rate/(2*n)*lo)<(bark-p->vi->noisewindowlo);lo++);
324
325 for(;hi<=n && (hi<i+p->vi->noisewindowhimin ||
326 toBARK(rate/(2*n)*hi)<(bark+p->vi->noisewindowhi));hi++);
327
328 p->bark[i]=((lo-1)<<16)+(hi-1);
329
330 }
331
332 /* set up rolling noise median */
333 p->noiseoffset=speex_alloc(n*sizeof(*p->noiseoffset));
334
335 for(i=0;i<n;i++){
336 float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
337 int inthalfoc;
338 float del;
339
340 if(halfoc<0)halfoc=0;
341 if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
342 inthalfoc=(int)halfoc;
343 del=halfoc-inthalfoc;
344
345 p->noiseoffset[i]=
346 p->vi->noiseoff[inthalfoc]*(1.-del) +
347 p->vi->noiseoff[inthalfoc+1]*del;
348
349 }
350 #if 0
351 _analysis_output_always("noiseoff0",ls,p->noiseoffset,n,1,0,0);
352 #endif
353
354 return p;
355 }
356
357 void vorbis_psy_destroy(VorbisPsy *p)
358 {
359 if(p){
360 spx_drft_clear(&p->lookup);
361 if(p->bark)
362 speex_free(p->bark);
363 if(p->noiseoffset)
364 speex_free(p->noiseoffset);
365 if(p->window)
366 speex_free(p->window);
367 memset(p,0,sizeof(*p));
368 speex_free(p);
369 }
370 }
371
372 void compute_curve(VorbisPsy *psy, float *audio, float *curve)
373 {
374 int i;
375 float work[psy->n];
376
377 float scale=4.f/psy->n;
378 float scale_dB;
379
380 scale_dB=todB(scale);
381
382 /* window the PCM data; use a BH4 window, not vorbis */
383 for(i=0;i<psy->n;i++)
384 work[i]=audio[i] * psy->window[i];
385
386 {
387 static int seq=0;
388
389 //_analysis_output("win",seq,work,psy->n,0,0);
390
391 seq++;
392 }
393
394 /* FFT yields more accurate tonal estimation (not phase sensitive) */
395 spx_drft_forward(&psy->lookup,work);
396
397 /* magnitudes */
398 work[0]=scale_dB+todB(work[0]);
399 for(i=1;i<psy->n-1;i+=2){
400 float temp = work[i]*work[i] + work[i+1]*work[i+1];
401 work[(i+1)>>1] = scale_dB+.5f * todB(temp);
402 }
403
404 /* derive a noise curve */
405 _vp_noisemask(psy,work,curve);
406 #define SIDEL 12
407 for (i=0;i<SIDEL;i++)
408 {
409 curve[i]=curve[SIDEL];
410 }
411 #define SIDEH 12
412 for (i=0;i<SIDEH;i++)
413 {
414 curve[(psy->n>>1)-i-1]=curve[(psy->n>>1)-SIDEH];
415 }
416 for(i=0;i<((psy->n)>>1);i++)
417 curve[i] = fromdB(1.2*curve[i]+.2*i);
418 //curve[i] = fromdB(0.8*curve[i]+.35*i);
419 //curve[i] = fromdB(0.9*curve[i])*pow(1.0*i+45,1.3);
420 }
421
422 /* Transform a masking curve (power spectrum) into a pole-zero filter */
423 void curve_to_lpc(VorbisPsy *psy, float *curve, float *awk1, float *awk2, int ord)
424 {
425 int i;
426 float ac[psy->n];
427 float tmp;
428 int len = psy->n >> 1;
429 for (i=0;i<2*len;i++)
430 ac[i] = 0;
431 for (i=1;i<len;i++)
432 ac[2*i-1] = curve[i];
433 ac[0] = curve[0];
434 ac[2*len-1] = curve[len-1];
435
436 spx_drft_backward(&psy->lookup, ac);
437 _spx_lpc(awk1, ac, ord);
438 tmp = 1.;
439 for (i=0;i<ord;i++)
440 {
441 tmp *= .99;
442 awk1[i] *= tmp;
443 }
444 #if 0
445 for (i=0;i<ord;i++)
446 awk2[i] = 0;
447 #else
448 /* Use the second (awk2) filter to correct the first one */
449 for (i=0;i<2*len;i++)
450 ac[i] = 0;
451 for (i=0;i<ord;i++)
452 ac[i+1] = awk1[i];
453 ac[0] = 1;
454 spx_drft_forward(&psy->lookup, ac);
455 /* Compute (power) response of awk1 (all zero) */
456 ac[0] *= ac[0];
457 for (i=1;i<len;i++)
458 ac[i] = ac[2*i-1]*ac[2*i-1] + ac[2*i]*ac[2*i];
459 ac[len] = ac[2*len-1]*ac[2*len-1];
460 /* Compute correction required */
461 for (i=0;i<len;i++)
462 curve[i] = 1. / (1e-6f+curve[i]*ac[i]);
463
464 for (i=0;i<2*len;i++)
465 ac[i] = 0;
466 for (i=1;i<len;i++)
467 ac[2*i-1] = curve[i];
468 ac[0] = curve[0];
469 ac[2*len-1] = curve[len-1];
470
471 spx_drft_backward(&psy->lookup, ac);
472 _spx_lpc(awk2, ac, ord);
473 tmp = 1;
474 for (i=0;i<ord;i++)
475 {
476 tmp *= .99;
477 awk2[i] *= tmp;
478 }
479 #endif
480 }
481
482 #if 0
483 #include <stdio.h>
484 #include <math.h>
485
486 #define ORDER 10
487 #define CURVE_SIZE 24
488
489 int main()
490 {
491 int i;
492 float curve[CURVE_SIZE];
493 float awk1[ORDER], awk2[ORDER];
494 for (i=0;i<CURVE_SIZE;i++)
495 scanf("%f ", &curve[i]);
496 for (i=0;i<CURVE_SIZE;i++)
497 curve[i] = pow(10.f, .1*curve[i]);
498 curve_to_lpc(curve, CURVE_SIZE, awk1, awk2, ORDER);
499 for (i=0;i<ORDER;i++)
500 printf("%f ", awk1[i]);
501 printf ("\n");
502 for (i=0;i<ORDER;i++)
503 printf("%f ", awk2[i]);
504 printf ("\n");
505 return 0;
506 }
507 #endif
508
509 #endif