applied AkhIL's cinelerra-cv surround patch
[cinelerra_cv/ct.git] / toolame-02l / psycho_0.c
blobf3c41f1aa017f9a43630c5203183249252340d6b
1 #include <stdio.h>
2 #include <math.h>
3 #include "common.h"
4 #include "ath.h"
5 #include "encoder.h"
6 #include "psycho_0.h"
8 /* MFC Mar 03
9 It's almost obscene how well this psycho model works for the amount of
10 computational effort that's put in.
12 I got the idea from:
13 Hyen-O Oh et al "Low power mpeg audio encoders using simplified psychoacoustic model
14 and fast bit allocation"
15 IEEE Trans on Consumer Electronics v47 n3 August 2001. p613
17 All this model does is look at the lowest ATH value within the subband, and then looks
18 at the scalefactors. It combines the two in a real dodgy way to get the SMRs.
20 Although the output values aren't really close to any of the other psycho models,
21 the spread of values and the relative sizes of the values for the different subbands
22 is about right
24 Feel free to make any sort of generic change you want. Add or subtract numbers, take
25 logs, whatever. Fiddle with the numbers until we get a good SMR output */
27 void psycho_0(double SMR[2][SBLIMIT], int nch, unsigned int scalar[2][3][SBLIMIT], FLOAT sfreq) {
28 int ch, sb, gr;
29 int minscaleindex[2][SBLIMIT]; /* Smaller scale indexes mean bigger scalefactors */
30 static FLOAT ath_min[SBLIMIT];
31 int i;
32 static int init=0;
34 if (!init) {
35 FLOAT freqperline = sfreq/1024.0;
36 for (sb=0;sb<SBLIMIT;sb++) {
37 ath_min[sb] = 1000; /* set it huge */
40 /* Find the minimum ATH in each subband */
41 for (i=0;i<512;i++) {
42 FLOAT thisfreq = i * freqperline;
43 FLOAT ath_val = ATH_dB(thisfreq, 0);
44 if (ath_val < ath_min[i>>4])
45 ath_min[i>>4] = ath_val;
47 init++;
50 /* Find the minimum scalefactor index for each ch/sb */
51 for (ch=0;ch<nch;ch++)
52 for (sb=0;sb<SBLIMIT;sb++)
53 minscaleindex[ch][sb] = scalar[ch][0][sb];
55 for (ch=0;ch<nch;ch++)
56 for (gr=1;gr<3;gr++)
57 for (sb=0;sb<SBLIMIT;sb++)
58 if (minscaleindex[ch][sb] > scalar[ch][gr][sb])
59 minscaleindex[ch][sb] = scalar[ch][gr][sb];
61 /* Oh yeah. Fudge the hell out of the SMR calculations
62 by combining the scalefactor table index and the min ATH in that subband
63 There are probably more elegant/correct ways of combining these values,
64 but who cares? It works pretty well
65 MFC Mar 03 */
66 for (ch=0;ch<nch;ch++)
67 for (sb=0;sb<SBLIMIT;sb++)
68 SMR[ch][sb] = 2.0 * (30.0 - minscaleindex[ch][sb]) - ath_min[sb];