File: psycho_4.c

package info (click to toggle)
twolame 0.3.12-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,436 kB
  • ctags: 1,064
  • sloc: sh: 9,013; ansic: 8,868; xml: 510; makefile: 135
file content (579 lines) | stat: -rw-r--r-- 16,616 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
 *	TwoLAME: an optimized MPEG Audio Layer Two encoder
 *
 *	Copyright (C) 2001-2004 Michael Cheng
 *	Copyright (C) 2004-2006 The TwoLAME Project
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	This library is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public
 *	License along with this library; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id: psycho_4.c 156 2007-03-20 23:57:35Z nhumfrey $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "twolame.h"
#include "common.h"
#include "mem.h"
#include "fft.h"
#include "ath.h"
#include "psycho_4.h"

/****************************************************************
PSYCHO_4 by MFC Feb 2003

This is a cleaned up implementation of psy model 2.
This is basically because I was sick of the inconsistencies between
the notation in the ISO docs and in the sourcecode.

I've nicked a bunch of stuff from LAME to make this a bit easier to grok
- ATH values (this also overcomes the lack of mpeg-2 tables
  which meant that LSF never had proper values)
- ath_freq2bark() to convert frequencies directly to bark values.
- spreading_function() isolated the calculation of the spreading function.
  Basically the same code as before, just isolated in its own function.
  LAME seem to does some extra tweaks to the ISO1117s model.
  Not really sure if they help or hinder, so I've commented them out (#ifdef LAME)

NB: Because of some of the tweaks to bark value calculation etc, it is now possible
to have 64 CBANDS. There's no real limit on the actual number of paritions. 
I wonder if it's worth experimenting with really higher numbers? Probably won't make
that much difference to the final SNR values, but it's something worth trying
	Maybe CBANDS should be a dynamic value, calculated by the psycho_init function
	CBANDS definition has been changed in encoder.h from 63 to 64

****************************************************************/


/* The static variables "r", "phi_sav", "new", "old" and "oldest" have	  
 to be remembered for the unpredictability measure.	 For "r" and		
 "phi_sav", the first index from the left is the channel select and		
 the second index is the "age" of the data.								*/


/* NMT is a constant 5.5dB. ISO11172 Sec D.2.4.h */
static const FLOAT NMT = 5.5;

/* The index into this array is a bark value 
   This array gives the 'minval' values from ISO11172 Tables D.3.x */
static const FLOAT minval[27] = {
  0.0, /* bark = 0 */
  20.0, /* 1 */
  20.0, /* 2 */
  20.0, /* 3 */
  20.0, /* 4 */
  20.0, /* 5 */
  17.0, /* 6 */
  15.0, /* 7 */
  10.0, /* 8 */
  7.0,	/* 9 */
  4.4,	/* 10 */
  4.5, 4.5, 4.5,4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5,	  /* 11 - 25 */
  3.5 /* 26 */
};


/* Table covers angles from	 0 to TRIGTABLESIZE/TRIGTABLESCALE (3.142) radians 
   In steps of 1/TRIGTABLESCALE (0.0005) radians. 
   Largest absolute error: 0.0005
   Only create a table for cos, and then use trig to work out sin.
   sin(theta) = cos(PI/2 - theta)
   MFC March 2003 */
static void psycho_4_trigtable_init(psycho_4_mem *p4mem) {

  int i;
  for (i=0;i<TRIGTABLESIZE;i++) {
	p4mem->cos_table[i] = cos((FLOAT)i/TRIGTABLESCALE);
  }
}

static inline FLOAT psycho_4_cos(psycho_4_mem *p4mem, FLOAT phi) {
  int index;
  int sign=1;

  index = (int)(fabs(phi) * TRIGTABLESCALE);
  while (index>=TRIGTABLESIZE) {
	/* If we're larger than PI, then subtract PI until we aren't
	   each time the sign will flip - Year 11 trig again. MFC March 2003 */
	index -= TRIGTABLESIZE;
	sign*=-1;
  }
  return(sign * p4mem->cos_table[index]);
}
/* The spreading function.	Values returned in units of energy
   Argument 'bark' is the difference in bark values between the
   centre of two partitions.
   This has been taken from LAME. MFC Feb 2003 */
static FLOAT psycho_4_spreading_function(FLOAT bark) {

	FLOAT tempx,x,tempy,temp;
	tempx = bark;
#ifdef LAME
	/* MP3 standard actually spreads these values a little more */
	if (tempx>=0) tempx *= 3;
	else tempx *=1.5;
#endif

	if(tempx>=0.5 && tempx<=2.5)
		{
			temp = tempx - 0.5;
			x = 8.0 * (temp*temp - 2.0 * temp);
		}
	else x = 0.0;
	tempx += 0.474;
	tempy = 15.811389 + 7.5*tempx - 17.5*sqrt(1.0+tempx*tempx);

	if (tempy <= -60.0) return	0.0;

	tempx = exp( (x + tempy)*LN_TO_LOG10 );

#ifdef LAME
	/* I'm not sure where the magic value of 0.6609193 comes from.
	   twolame will just keep using the rnorm to normalise the spreading function
	   MFC Feb 2003 */
	/* Normalization.  The spreading function should be normalized so that:
		 +inf
		   /
		   |  s3 [ bark ]  d(bark)	 =	1
		   /
		 -inf
	*/
	tempx /= .6609193;
#endif
	return tempx;

}

/********************************
 * init psycho model 2
 ********************************/
static psycho_4_mem *psycho_4_init (twolame_options *glopts, int sfreq)
{
  psycho_4_mem *mem;
  FLOAT *cbval, *rnorm;
  FLOAT *window;
  FLOAT bark[HBLKSIZE], *ath;
  int *numlines;
  int *partition;
  FCB *s;
  FLOAT *tmn;
  int i, j;

  {
	mem = (psycho_4_mem *)TWOLAME_MALLOC(sizeof(psycho_4_mem));

	mem->tmn = (FLOAT *) TWOLAME_MALLOC (sizeof (DCB));
	mem->s = (FCB *) TWOLAME_MALLOC (sizeof (FCBCB));
	mem->lthr = (FHBLK *) TWOLAME_MALLOC (sizeof (F2HBLK));
	mem->r = (F2HBLK *) TWOLAME_MALLOC (sizeof (F22HBLK));
	mem->phi_sav = (F2HBLK *) TWOLAME_MALLOC (sizeof (F22HBLK));

	mem->new=0;
	mem->old=1;
	mem->oldest=0;
  }

  {
	cbval = mem->cbval;
	rnorm = mem->rnorm;
	window = mem->window;
	//bark = mem->bark;
	ath = mem->ath;
	numlines = mem->numlines;
	partition = mem->numlines;
	s = mem->s;
	tmn = mem->tmn;
  }


  /* Set up the SIN/COS tables */
  psycho_4_trigtable_init(mem);

  /* calculate HANN window coefficients */
  for (i = 0; i < BLKSIZE; i++)
	window[i] = 0.5 * (1 - cos (2.0 * PI * (i - 0.5) / BLKSIZE));

  /* For each FFT line from 0(DC) to 512(Nyquist) calculate
	 - bark			 : the bark value of this fft line
	 - ath		  : the absolute threshold of hearing for this line [ATH]  

	 Since it is a 1024 point FFT, each line in the fft	 corresponds 
	 to	  1/1024 of the total frequency.	
	 Line 0 should correspond to DC - which doesn't really have a ATH afaik
	 Line 1 should be 1/1024th of the Sampling Freq
	 Line 512 should be the nyquist freq  */
  for (i=0; i<HBLKSIZE; i++) {
	FLOAT freq = i * (FLOAT)sfreq/(FLOAT)BLKSIZE;
	bark[i] = ath_freq2bark(freq);
	/* The ath tables in the dist10 code seem to be a little out of kilter. 
	   they seem to start with index 0 corresponding to (sampling freq)/1024.
	   When in doubt, i'm going to assume that the dist10 code is wrong. MFC Feb2003  */
	ath[i] = ath_energy(freq,glopts->athlevel);
	//fprintf(stderr,"%.2f ",ath[i]);
  }	 
 

  /* Work out the partitions
	 Starting from line 0, all lines within 0.33 of the starting
	 bark are added to the same partition. When a line is greater
	 by 0.33 of a bark, start a new partition.	*/
  {
	int partition_count = 0; /* keep a count of the partitions */
	int cbase = 0; /* current base index for the bark range calculation */
	for (i=0;i<HBLKSIZE;i++) {
	  if ((bark[i] - bark[cbase]) > 0.33) { /* 1/3 critical band? */
	/* this frequency line is too different from the starting line,
	   (in terms of the bark distance)
	   so close that previous partition, and make this line the first
	   member of the next partition */
	cbase = i; /* Start the new partition from this frequency */
	partition_count++;
	  } 
	  /* partition[i] tells us which partition the i'th frequency line is in */
	  partition[i] = partition_count;
	  /* keep a count of how many frequency lines are in each partition */
	  numlines[partition_count]++;
	}
  }

  /* For each partition within the frequency space, 
	 calculate the average bark value - cbval [central bark value] */
  for (i=0;i<HBLKSIZE;i++) 
	cbval[partition[i]] += bark[i]; /* sum up all the bark values */
  for (i=0;i<CBANDS;i++) {
	if (numlines[i] != 0)
	  cbval[i] /= numlines[i]; /* divide by the number of values */
	else {
	  cbval[i]=0; /* this isn't a partition */
	}
  }
  

  /* Calculate the spreading function. ISO 11172 Section D.2.3 */
  for (i=0;i<CBANDS;i++) {
	for (j=0;j<CBANDS;j++) {
	  s[i][j] = psycho_4_spreading_function( 1.05 * (cbval[i] - cbval[j]) );
	  rnorm[i] += s[i][j]; /* sum the spreading function values for each partition so that
				they can be normalised later on */
	}
  }
  
  /* Calculate Tone Masking Noise values. ISO 11172 Tables D.3.x */
  for (j = 0; j < CBANDS; j++)
	tmn[j] = MAX(15.5+cbval[j], 24.5);


  if (glopts->verbosity > 6) {
	/* Dump All the Values to STDOUT */
	int wlow, whigh=0;
	int ntot=0;
	fprintf(stdout,"psy model 4 init\n");
	fprintf(stdout,"index \tnlines \twlow \twhigh \tbval \tminval \ttmn\n");
	for (i=0;i<CBANDS;i++) 
	  if (numlines[i] != 0) {
	  wlow = whigh+1;
	  whigh = wlow + numlines[i] - 1;
	  fprintf(stdout,"%i \t%i \t%i \t%i \t%5.2f \t%4.2f \t%4.2f\n",i+1, numlines[i],wlow, whigh, cbval[i],minval[(int)cbval[i]],tmn[i]);
	  ntot += numlines[i];
	  }
	fprintf(stdout,"total lines %i\n",ntot);
	exit(0);
  }

  return(mem);
}


void psycho_4 (twolame_options *glopts, 
		short int buffer[2][1152],
		short int savebuf[2][1056],
		FLOAT smr[2][32])
/* to match prototype : FLOAT args are always FLOAT */
{
  psycho_4_mem *mem;
  unsigned int run, i, j, k, ch;
  FLOAT r_prime, phi_prime;
  FLOAT npart, epart;
  int new, old, oldest;
  FLOAT *grouped_c, *grouped_e;
  FLOAT *nb, *cb, *tb, *ecb, *bc;
  FLOAT *cbval, *rnorm;
  FLOAT *wsamp_r, *phi, *energy, *window;
  FLOAT *ath, *thr, *c;

  FLOAT *snrtmp[2];
  int *numlines;
  int *partition;
  FLOAT *tmn;
  FCB *s;
  FHBLK *lthr;
  F2HBLK *r, *phi_sav;
  
  int nch = glopts->num_channels_out;
  int sfreq = glopts->samplerate_out;

  if (!glopts->p4mem) {
	glopts->p4mem = psycho_4_init (glopts,sfreq);
  }

  mem = glopts->p4mem;
  {
	grouped_c = mem->grouped_c;
	grouped_e = mem->grouped_e;
	nb = mem->nb;
	cb = mem->cb;
	tb = mem->tb;
	ecb = mem->ecb;
	bc = mem->bc;
	rnorm = mem->rnorm;
	cbval = mem->cbval;
	wsamp_r = mem->wsamp_r;
	phi = mem->phi;
	energy = mem->energy;
	window = mem->window;
	ath = mem->ath;
	thr = mem->thr;
	c = mem->c;

	snrtmp[0] = mem->snrtmp[0];
	snrtmp[1] = mem->snrtmp[1];

	numlines = mem->numlines;
	partition = mem->partition;
	tmn = mem->tmn;
	s = mem->s;
	lthr = mem->lthr;
	r = mem->r;
	phi_sav = mem->phi_sav;	   
  }

  for (ch = 0; ch < nch; ch++) {
  for (run = 0; run < 2; run++) {
	/* Net offset is 480 samples (1056-576) for layer 2; this is because one must
	   stagger input data by 256 samples to synchronize psychoacoustic model with
	   filter bank outputs, then stagger so that center of 1024 FFT window lines 
	   up with center of 576 "new" audio samples.								 
	   
	   flush = 384*3.0/2.0;	 = 576
	   syncsize = 1056;
	   sync_flush = syncsize - flush;	480
	   BLKSIZE = 1024											   */
	{
	  short int *bufferp = buffer[ch];
	  for (j = 0; j < 480; j++) {
	savebuf[ch][j] = savebuf[ch][j + 576];
	wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
	  }
	  for (; j < 1024; j++) {
	savebuf[ch][j] = *bufferp++;
	wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
	  }
	  for (; j < 1056; j++)
	savebuf[ch][j] = *bufferp++;
	}

	/* Compute FFT */
	psycho_2_fft (wsamp_r, energy, phi);

	/* calculate the unpredictability measure, given energy[f] and phi[f] 
	   (the age pointers [new/old/oldest] are reset automatically on the second pass */
	{
	  if (mem->new == 0) {
	mem->new = 1;
	mem->oldest = 1;
	  } else {
	mem->new = 0;
	mem->oldest = 0;
	  }
	  if (mem->old == 0)
	mem->old = 1;
	  else
	mem->old = 0;
	}
	old = mem->old;
	new = mem->new;
	oldest = mem->oldest;


	for (j = 0; j < HBLKSIZE; j++) {
#ifdef NEWATAN
	  FLOAT temp1, temp2, temp3;
	  r_prime = 2.0 * r[ch][old][j] - r[ch][oldest][j];
	  phi_prime = 2.0 * phi_sav[ch][old][j] - phi_sav[ch][oldest][j];

	  r[ch][new][j] = sqrt ((FLOAT) energy[j]);
	  phi_sav[ch][new][j] = phi[j]; 
  
	  {
	temp1 =
	  r[ch][new][j] * psycho_4_cos(mem, phi[j]) -
	  r_prime * psycho_4_cos(mem, phi_prime);
	/* Remember your grade 11 trig? sin(theta) = cos(PI/2 - theta) */
	temp2 =
	  r[ch][new][j] * psycho_4_cos(mem, PI2 - phi[j]) - 
	  r_prime * psycho_4_cos(mem, PI2 - phi_prime); 
	  }


	  temp3 = r[ch][new][j] + fabs ((FLOAT) r_prime);
	  if (temp3 != 0)
	c[j] = sqrt (temp1 * temp1 + temp2 * temp2) / temp3;
	  else
	c[j] = 0;
#else
	  FLOAT temp1, temp2, temp3;
	  r_prime = 2.0 * r[ch][old][j] - r[ch][oldest][j];
	  phi_prime = 2.0 * phi_sav[ch][old][j] - phi_sav[ch][oldest][j];

	  r[ch][new][j] = sqrt ((FLOAT) energy[j]);
	  phi_sav[ch][new][j] = phi[j]; 


	  temp1 =
	r[ch][new][j] * cos ((FLOAT) phi[j]) -
	r_prime * cos ((FLOAT) phi_prime);
	  temp2 =
	r[ch][new][j] * sin ((FLOAT) phi[j]) -
	r_prime * sin ((FLOAT) phi_prime);		

	  temp3 = r[ch][new][j] + fabs ((FLOAT) r_prime);
	  if (temp3 != 0)
	c[j] = sqrt (temp1 * temp1 + temp2 * temp2) / temp3;
	  else
	c[j] = 0;
#endif
	}

	/* For each partition, sum all the energy in that partition - grouped_e
	   and calculated the energy-weighted unpredictability measure - grouped_c
	   ISO 11172 Section D.2.4.e */
	for (j = 1; j < CBANDS; j++) {
	  grouped_e[j] = 0;
	  grouped_c[j] = 0;
	}
	grouped_e[0] = energy[0];
	grouped_c[0] = energy[0] * c[0];
	for (j = 1; j < HBLKSIZE; j++) {
	  grouped_e[partition[j]] += energy[j];
	  grouped_c[partition[j]] += energy[j] * c[j];
	}

	/* convolve the grouped energy-weighted unpredictability measure			 
	   and the grouped energy with the spreading function
	   ISO 11172 D.2.4.f */
	for (j = 0; j < CBANDS; j++) {
	  ecb[j] = 0;
	  cb[j] = 0;
	  for (k = 0; k < CBANDS; k++) {
	if (s[j][k] != 0.0) {
	  ecb[j] += s[j][k] * grouped_e[k];
	  cb[j] += s[j][k] * grouped_c[k];
	}
	  }
	  if (ecb[j] != 0)
	cb[j] = cb[j] / ecb[j];
	  else
	cb[j] = 0;
	}

	/* Convert cb to tb (the tonality index) 
	   ISO11172 SecD.2.4.g */
	for (i=0;i<CBANDS;i++) {
	  if (cb[i] < 0.05)
	cb[i] = 0.05;
	  else if (cb[i] > 0.5)
	cb[i] = 0.5;
	  tb[i] = -0.301029996 - 0.434294482 * log((FLOAT) cb[i]);
	}
	  

	/* Calculate the required SNR for each of the frequency partitions 
	   ISO 11172 Sect D.2.4.h */
	for (j = 0; j < CBANDS; j++) {
	  FLOAT SNR, SNRtemp;
	  SNRtemp = tmn[j] * tb[j] + NMT * (1.0 - tb[j]);
	  SNR = MAX(SNRtemp, minval[(int)cbval[j]]);
	  bc[j] = exp ((FLOAT) -SNR * LN_TO_LOG10);
	}

	/* Calculate the permissible noise energy level in each of the frequency	 
	   partitions. 
	   This section used to have pre-echo control but only for LayerI 
	   ISO 11172 Sec D.2.4.k - Spread the threshold energy over FFT lines */
	for (j = 0; j < CBANDS; j++) {
	  if (rnorm[j] && numlines[j])
	nb[j] = ecb[j] * bc[j] / (rnorm[j] * numlines[j]);
	  else
	nb[j] = 0;
	}

	/* ISO11172 Sec D.2.4.l - thr[] the final energy threshold of audibility */
	for (j = 0; j < HBLKSIZE; j++) 
	  thr[j] = MAX(nb[partition[j]], ath[j]);

	/* Translate the 512 threshold values to the 32 filter bands of the coder  
	   Using ISO 11172 Table D.5 and Section D.2.4.n */
	for (j = 0; j < 193; j += 16) {
	  /* WIDTH = 0 */
	  npart = 60802371420160.0;
	  epart = 0.0;
	  for (k = 0; k < 17; k++) {
	if (thr[j + k] < npart)
	  npart = thr[j + k]; /* For WIDTH==0, find the minimum noise, and 
					 later multiply by the number of indexes i.e. 17 */
	epart += energy[j + k];
	  }
	  snrtmp[run][j / 16] = 4.342944819 * log((FLOAT)(epart/(npart*17.0)));
	}
	for (j = 208; j < (HBLKSIZE - 1); j += 16) {
	  /* WIDTH = 1 */
	  npart = 0.0;
	  epart = 0.0;
	  for (k = 0; k < 17; k++) {
	npart += thr[j + k]; /* For WIDTH==1, sum the noise */
	epart += energy[j + k];
	  }
	  snrtmp[run][j / 16] = 4.342944819 * log ((FLOAT) (epart/npart));
	}
  }

  /* Pick the maximum value of the two runs ISO 11172 Sect D.2.1 */
  for (i = 0; i < 32; i++) 
	smr[ch][i] = MAX(snrtmp[0][i], snrtmp[1][i]);

  } // now do other channel
  
}


void psycho_4_deinit(psycho_4_mem **mem) {

	if (mem==NULL||*mem==NULL) return;

	TWOLAME_FREE( (*mem)->tmn );
	TWOLAME_FREE( (*mem)->s );
	TWOLAME_FREE( (*mem)->lthr );
	TWOLAME_FREE( (*mem)->r );
	TWOLAME_FREE( (*mem)->phi_sav );

	TWOLAME_FREE( (*mem) );
}



// vim:ts=4:sw=4:nowrap: