2 (c) Copyright 1998-2001 - Tord Jansson
3 ======================================
5 This file is part of the BladeEnc MP3 Encoder, based on
6 ISO's reference code for MPEG Layer 3 compression, and might
7 contain smaller or larger sections that are directly taken
8 from ISO's reference code.
10 All changes to the ISO reference code herein are either
11 copyrighted by Tord Jansson (tord.jansson@swipnet.se)
12 or sublicensed to Tord Jansson by a third party.
14 BladeEnc is free software; you can redistribute this file
15 and/or modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 2.1 of the License, or (at your option) any later version.
21 ------------ Changes ------------
23 2000-12-05 Andre Piotrowski
25 - speed up: implemented prepacking of fft-data
29 - speed up: faster psycho_anal()
30 - optional bug fix: integrated better norm calclulation and block selecting
34 - use SHORT_FFT_MIN_IDX to allow switching of "ORG_SHORT_CW_LIMIT" in "l3psy.h"
38 - use some explicit type casting to avoid compiler warnings
59 /*____ Global Static Variables ______________________________________________*/
61 /* The static variables "r", "phi_sav", "new_", "old" and "oldest" have */
62 /* to be remembered for the unpredictability measure. For "r" and */
63 /* "phi_sav", the first index from the left is the channel select and */
64 /* the second index is the "age" of the data. */
67 static int new_
, old
, oldest
;
68 static int flush
, sync_flush
, syncsize
;
71 static int savebuf_start_idx
[2];
78 static double *minval
, *qthr_l
;
79 static double *qthr_s
, *SNR_s
;
80 static int *cbw_l
, *bu_l
, *bo_l
;
81 static int *cbw_s
, *bu_s
, *bo_s
;
82 static double *w1_l
, *w2_l
;
83 static double *w1_s
, *w2_s
;
87 static int cbmax_l
= CBANDS
, cbmax_s
= CBANDS_s
;
88 static int numlines_l
[CBANDS
];
90 static int partition_l
[HBLKSIZE
];
91 static int partition_s
[HBLKSIZE_s
];
92 static double s3_l
[CBANDS
][CBANDS
];
93 static double *norm_l
, *norm_s
;
97 static int cbmax_l
, cbmax_s
;
98 static int *numlines_l
;
99 static int *numlines_s
;
101 /* the non-zero entries of norm_l[i] * s3_l[i][j] */
102 static FLOAT normed_s3_l
[900]; /* a bit more space than needed [799|855|735] */
103 static int lo_s3_l
[CBANDS
];
104 static int hi_s3_l
[CBANDS
];
106 static FLOAT normed_s3_s
[500]; /* a bit more space than needed [445|395|378] */
107 static int lo_s3_s
[CBANDS_s
];
108 static int hi_s3_s
[CBANDS_s
];
110 #endif /* ORG_NUMLINES_NORM */
114 static double minval
[CBANDS
], qthr_l
[CBANDS
], norm_l
[CBANDS
];
115 static double qthr_s
[CBANDS_s
], norm_s
[CBANDS_s
], SNR_s
[CBANDS_s
];
116 static int cbw_l
[SBMAX_l
],bu_l
[SBMAX_l
],bo_l
[SBMAX_l
];
117 static int cbw_s
[SBMAX_s
],bu_s
[SBMAX_s
],bo_s
[SBMAX_s
];
118 static double w1_l
[SBMAX_l
], w2_l
[SBMAX_l
];
119 static double w1_s
[SBMAX_s
], w2_s
[SBMAX_s
];
121 static int numlines_l
[CBANDS
];
123 static int partition_l
[HBLKSIZE
];
124 static int partition_s
[HBLKSIZE_s
];
125 static double s3_l
[CBANDS
][CBANDS
];
127 #endif /* NEW_L3PARM_TABLES */
131 /* Scale Factor Bands */
132 static int blocktype_old
[2];
136 static double nb_1
[2][CBANDS
];
137 static double nb_2
[2][CBANDS
];
139 static double cw
[HBLKSIZE
];
141 static FLOAT window
[BLKSIZE
];
142 static FLOAT r
[2][2][6];
143 static FLOAT phi_sav
[2][2][6];
145 static FLOAT window_s
[BLKSIZE_s
];
147 static double ratio
[2][SBMAX_l
];
148 static double ratio_s
[2][SBMAX_s
][3];
154 #if NEW_L3PARM_TABLES
156 static void L3para_read (int sfreq
);
158 #if !ORG_NUMLINES_NORM
159 static void calc_normed_spreading
161 int cbmax
, /* number of lines and rows */
162 const double bval
[], /* input values to compute the matrix */
163 FLOAT s3_ptr
[], /* the resulting non-zero entries */
172 static void L3para_read
175 int numlines_l
[CBANDS
],
176 int partition_l
[HBLKSIZE
],
177 double minval
[CBANDS
],
178 double qthr_l
[CBANDS
],
179 double norm_l
[CBANDS
],
180 double s3_l
[CBANDS
][CBANDS
],
181 int partition_s
[HBLKSIZE_s
],
182 double qthr_s
[CBANDS_s
],
183 double norm_s
[CBANDS_s
],
184 double SNR_s
[CBANDS_s
],
188 double w1_l
[SBMAX_l
],
189 double w2_l
[SBMAX_l
],
193 double w1_s
[SBMAX_s
],
203 /*____ psycho_anal_init() ___________________________________________________*/
205 void psycho_anal_init (double sfreq
)
207 unsigned int ch
, sfb
, b
, i
, j
;
210 /* reset the r, phi_sav "ring buffer" indices */
212 old
= 1 - (new_
= oldest
= 0);
215 /* clear the ratio arrays */
217 for (ch
= 0; ch
< 2; ch
++)
219 for (sfb
= 0; sfb
< SBMAX_l
; sfb
++)
220 ratio
[ch
][sfb
] = 0.0;
222 for (sfb
= 0; sfb
< SBMAX_s
; sfb
++)
223 for (b
= 0; b
< 3; b
++)
224 ratio_s
[ch
][sfb
][b
] = 0.0;
228 /* clear preecho arrays */
230 for (ch
= 0; ch
< 2; ch
++)
232 for (i
= 0; i
< CBANDS
; i
++)
240 /* clear blocktype information */
242 for (ch
= 0; ch
< 2; ch
++)
243 blocktype_old
[ch
] = NORM_TYPE
;
248 syncsize
= 1344; /* sync_flush + flush */
252 for (ch
= 0; ch
< 2; ch
++)
253 savebuf_start_idx
[ch
] = 0;
257 /* calculate HANN window coefficients */
259 for (i
= 0; i
< BLKSIZE
; i
++)
260 window
[i
] = (FLOAT
) (0.5 * (1 - cos (2.0 * PI
* (i
- 0.5) / BLKSIZE
)));
262 for (i
= 0; i
< BLKSIZE_s
; i
++)
263 window_s
[i
] = (FLOAT
) (0.5 * (1 - cos (2.0 * PI
* (i
- 0.5) / BLKSIZE_s
)));
266 /* reset states used in unpredictability measure */
268 for (ch
= 0; ch
< 2; ch
++)
270 for (i
= 0; i
< 2; i
++)
272 for (j
= 0; j
< 6; j
++)
275 phi_sav
[ch
][i
][j
] = 0.0;
281 #if NEW_L3PARM_TABLES
282 L3para_read ((int) sfreq
);
287 numlines_l
, partition_l
, minval
, qthr_l
, norm_l
, s3_l
,
288 partition_s
, qthr_s
, norm_s
, SNR_s
,
289 cbw_l
, bu_l
, bo_l
, w1_l
, w2_l
,
290 cbw_s
, bu_s
, bo_s
, w1_s
, w2_s
295 /* Set unpredicatiblility of remaining spectral lines to 0.4 */
297 for (j
= 206; j
< HBLKSIZE
; j
++)
305 /*____ psycho_anal_exit() ___________________________________________________*/
307 void psycho_anal_exit( void )
316 /*____ psycho_anal() ________________________________________________________*/
322 short int savebuf
[2048],
329 /* float snr32[32], */
330 double ratio_d
[SBMAX_l
],
331 double ratio_ds
[SBMAX_s
][3],
337 unsigned int sfb
, b
, j
, k
;
338 double r_prime
, phi_prime
; /* not FLOAT */
339 double temp1
, temp2
, temp3
;
341 #if !ORG_NUMLINES_NORM && NEW_L3PARM_TABLES
350 FLOAT wsamp_r
[HBLKSIZE
];
351 FLOAT wsamp_i
[HBLKSIZE
];
353 FLOAT energy
[HBLKSIZE
];
355 FLOAT energy_s
[3][BLKSIZE_s
];
363 # define savebuf buffer
364 # define beg buffer_idx
369 for (sfb
= 0; sfb
< SBMAX_l
; sfb
++)
370 ratio_d
[sfb
] = ratio
[ch
][sfb
];
372 for (sfb
= 0; sfb
< SBMAX_s
; sfb
++)
373 for (b
= 0; b
< 3; b
++)
374 ratio_ds
[sfb
][b
] = ratio_s
[ch
][sfb
][b
];
378 old
= 1 - (new_
= oldest
= old
);
382 /**********************************************************************
383 * Delay signal by sync_flush=768 samples *
384 **********************************************************************/
387 for (j
= 0; j
< sync_flush
; j
++) /* for long window samples */
388 savebuf
[j
] = savebuf
[j
+flush
];
390 for (j
= sync_flush
; j
< syncsize
; j
++)
391 savebuf
[j
] = *buffer
++;
393 beg
= savebuf_start_idx
[ch
] = (savebuf_start_idx
[ch
] + flush
) & 2047;
395 idx
= (beg
+ sync_flush
) & 2047;
396 fin
= (idx
+ flush
) & 2047;
400 savebuf
[idx
++] = *buffer
++;
404 savebuf
[idx
++] = *buffer
++;
409 /**********************************************************************
410 * compute unpredicatability of first six spectral lines *
411 **********************************************************************/
414 for (j
= 0, k
= 0, idx
= 0; j
< BLKSIZE
/2; j
++)
416 wsamp_r
[j
] = window
[k
++] * savebuf
[idx
++];
417 wsamp_i
[j
] = window
[k
++] * savebuf
[idx
++];
422 fin
= (idx
+ BLKSIZE
) & 2047;
427 wsamp_r
[j
] = window
[k
++] * savebuf
[idx
++];
428 wsamp_i
[j
] = window
[k
++] * savebuf
[idx
++];
435 wsamp_r
[j
] = window
[k
++] * savebuf
[idx
++];
436 wsamp_i
[j
] = window
[k
++] * savebuf
[idx
++];
441 fft(wsamp_r
, wsamp_i
, energy
, phi
, BLKSIZE
); /* long FFT */
443 for (j
= 0; j
< 6; j
++)
444 { /* calculate unpredictability measure cw */
446 r_prime
= 2.0 * r
[ch
][old
][j
] - r
[ch
][oldest
][j
];
447 phi_prime
= 2.0 * phi_sav
[ch
][old
][j
] - phi_sav
[ch
][oldest
][j
];
448 r
[ch
][new_
][j
] = (FLOAT
) ( r1
= sqrt((double) energy
[j
]));
449 phi_sav
[ch
][new_
][j
] = (FLOAT
) (phi1
= phi
[j
] );
451 temp3
= r1
+ fabs(r_prime
);
454 temp1
= r1
* cos(phi1
) - r_prime
* cos(phi_prime
);
455 temp2
= r1
* sin(phi1
) - r_prime
* sin(phi_prime
);
456 cw
[j
] = sqrt(temp1
*temp1
+ temp2
*temp2
) / temp3
;
463 /**********************************************************************
464 * compute unpredicatibility of next 200 spectral lines *
465 **********************************************************************/
467 for (b
= 0; b
< 3; b
++)
470 for (j
= 0, k
= 0, idx
= 128*(2 + b
); j
< BLKSIZE_s
/2; j
++)
471 { /* window data with HANN window */
472 wsamp_r
[j
] = window_s
[k
++] * savebuf
[idx
++];
473 wsamp_i
[j
] = window_s
[k
++] * savebuf
[idx
++];
477 idx
= (beg
+ 128*(2 + b
)) & 2047;
478 fin
= (idx
+ BLKSIZE_s
) & 2047;
483 wsamp_r
[j
] = window_s
[k
++] * savebuf
[idx
++];
484 wsamp_i
[j
] = window_s
[k
++] * savebuf
[idx
++];
491 wsamp_r
[j
] = window_s
[k
++] * savebuf
[idx
++];
492 wsamp_i
[j
] = window_s
[k
++] * savebuf
[idx
++];
497 fft (wsamp_r
, wsamp_i
, energy_s
[b
], phi_s
[b
], BLKSIZE_s
); /* short FFT*/
500 for (j
= 6, k
= SHORT_FFT_MIN_IDX
; j
< 206; j
+= 4, k
++)
501 { /* calculate unpredictability measure cw */
504 r_prime
= 2.0 * sqrt((double) energy_s
[0][k
]) - sqrt((double) energy_s
[2][k
]);
505 phi_prime
= 2.0 * phi_s
[0][k
] - phi_s
[2][k
];
506 r1
= sqrt((double) energy_s
[1][k
]);
509 temp3
= r1
+ fabs(r_prime
);
512 temp1
= r1
* cos(phi1
) - r_prime
* cos(phi_prime
);
513 temp2
= r1
* sin(phi1
) - r_prime
* sin(phi_prime
);
514 cw
[j
] = sqrt(temp1
*temp1
+ temp2
*temp2
) / temp3
;
519 cw
[j
+1] = cw
[j
+2] = cw
[j
+3] = cw
[j
];
523 /**********************************************************************
524 * Calculate the energy and the unpredictability in the threshold *
525 * calculation partitions *
526 **********************************************************************/
528 #if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
530 for (b
= 0; b
< cbmax_l
; b
++)
535 for (j
= 0; j
< HBLKSIZE
; j
++)
537 int tp
= partition_l
[j
];
541 cb
[tp
] += cw
[j
] * energy
[j
];
548 for (b
= 0; b
< cbmax_l
; b
++)
554 Calculate the energy and the unpredictability in the threshold
555 calculation partitions
557 cbmax_l holds the number of valid numlines_l entries
562 cb
[b
] += cw
[j
] * energy
[j
];
566 s3_ptr
= normed_s3_l
;
573 for (b
= 0; b
< cbmax_l
; b
++)
583 convolve the partitioned energy and unpredictability
584 with the spreading function, normed_s3_l[b][k]
586 #if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
587 for (k
= 0; k
< cbmax_l
; k
++)
589 ecb
+= s3_l
[b
][k
] * eb
[k
]; /* sprdngf for Layer III */
590 ctb
+= s3_l
[b
][k
] * cb
[k
];
593 for (k
= lo_s3_l
[b
]; k
< hi_s3_l
[b
]; k
++)
595 ecb
+= *s3_ptr
* eb
[k
]; /* sprdngf for Layer III */
596 ctb
+= *s3_ptr
++ * cb
[k
];
602 calculate the tonality of each threshold calculation partition
603 calculate the SNR in each threshhold calculation partition
610 tbb
= -0.299 - 0.43 * log(cbb
); /* conv1=-0.299, conv2=-0.43 */
611 tbb
= MIN(MAX (0.0, tbb
), 1.0) ; /* 0<=tbb<=1 */
614 tbb
= 0.0; /* cbb==0 => -0.299-0.43*cbb<0 => tbb=0*/
616 /* TMN=29.0,NMT=6.0 for all calculation partitions */
617 SNR_l
= MAX (minval
[b
], 23.0 * tbb
+ 6.0); /* 29*tbb + 6*(1-tbb) */
619 /* calculate the threshold for each partition */
620 #if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
621 nb
= ecb
* norm_l
[b
] * exp(-SNR_l
* LN_TO_LOG10
);
623 nb
= ecb
* exp(-SNR_l
* LN_TO_LOG10
); /* our ecb is already normed */
629 thr
[b
] = MAX (qthr_l
[b
], MIN(nb
, nb_2
[ch
][b
]));
630 nb_2
[ch
][b
] = MIN(2.0 * nb
, 16.0 * nb_1
[ch
][b
]);
635 calculate percetual entropy
637 thr[b] -> thr[b]+1.0 : for non sound portition
640 *pe
+= numlines_l
[b
] * log((eb
[b
]+1.0) / (thr
[b
]+1.0));
644 #define switch_pe 1800
649 /* no attack : use long blocks */
651 if (blocktype_old
[ch
] == SHORT_TYPE
)
652 blocktype
= STOP_TYPE
;
653 else /* NORM_TYPE, STOP_TYPE */
654 blocktype
= NORM_TYPE
;
657 /* threshold calculation (part 2) */
659 for (sfb
= 0; sfb
< SBMAX_l
; sfb
++)
663 double en
= w1_l
[sfb
] * eb
[bu
] + w2_l
[sfb
] * eb
[bo
];
665 for (b
= bu
+1; b
< bo
; b
++)
670 double thm
= w1_l
[sfb
] * thr
[bu
] + w2_l
[sfb
] * thr
[bo
];
672 for (b
= bu
+1; b
< bo
; b
++)
675 ratio
[ch
][sfb
] = thm
/ en
;
678 ratio
[ch
][sfb
] = 0.0;
683 /* attack : use short blocks */
684 blocktype
= SHORT_TYPE
;
686 if (blocktype_old
[ch
] == NORM_TYPE
)
687 blocktype_old
[ch
] = START_TYPE
;
688 else /* SHORT_TYPE, STOP_TYPE */
689 blocktype_old
[ch
] = SHORT_TYPE
;
691 if (blocktype_old
[ch
] == SHORT_TYPE
)
692 blocktype_old
[ch
] = SHORT_TYPE
;
693 else /* NORM_TYPE, STOP_TYPE */
694 blocktype_old
[ch
] = START_TYPE
;
698 /* threshold calculation for short blocks */
700 for (sblock
= 0; sblock
< 3; sblock
++)
702 #if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
704 for (b
= 0; b
< cbmax_s
; b
++)
707 for (j
= 0; j
< HBLKSIZE_s
; j
++)
708 eb
[partition_s
[j
]] += energy_s
[sblock
][j
];
713 for (b
= 0; b
< cbmax_s
; b
++)
718 Calculate the energy and the unpredictability in the threshold
719 calculation partitions
721 cbmax_s holds the number of valid numlines_s entries
725 eb
[b
] += energy_s
[sblock
][j
];
729 s3_ptr
= normed_s3_s
;
732 for (b
= 0; b
< cbmax_s
; b
++)
737 #if ORG_NUMLINES_NORM || !NEW_L3PARM_TABLES
738 for (k
= 0; k
< cbmax_s
; k
++)
739 ecb
+= s3_l
[b
][k
] * eb
[k
];
741 nb
= ecb
* norm_l
[b
] * exp((double) SNR_s
[b
] * LN_TO_LOG10
);
743 for (k
= lo_s3_s
[b
]; k
< hi_s3_s
[b
]; k
++)
744 ecb
+= *s3_ptr
++ * eb
[k
];
746 nb
= ecb
* exp((double) SNR_s
[b
] * LN_TO_LOG10
); /* our ecb is already normed */
748 thr
[b
] = MAX(qthr_s
[b
], nb
);
751 for (sfb
= 0; sfb
< SBMAX_s
; sfb
++)
755 double en
= w1_s
[sfb
] * eb
[bu
] + w2_s
[sfb
] * eb
[bo
];
757 for (b
= bu
+1; b
< bo
; b
++)
761 double thm
= w1_s
[sfb
] * thr
[bu
] + w2_s
[sfb
] * thr
[bo
];
763 for (b
= bu
+1; b
< bo
; b
++)
766 ratio_s
[ch
][sfb
][sblock
] = thm
/ en
;
769 ratio_s
[ch
][sfb
][sblock
] = 0.0;
774 cod_info
->block_type
= blocktype_old
[ch
];
775 blocktype_old
[ch
] = blocktype
;
777 if ( cod_info
->block_type
== NORM_TYPE
)
778 cod_info
->window_switching_flag
= 0;
780 cod_info
->window_switching_flag
= 1;
782 cod_info
->mixed_block_flag
= 0;
789 /*____ L3para_read() __________________________________________________________*/
791 #if NEW_L3PARM_TABLES
793 static void L3para_read (int sfreq
)
797 double *bval_l
, *bval_s
;
799 #if ORG_NUMLINES_NORM
800 int cbmax_l
, cbmax_s
;
803 double *norm_l
, *norm_s
;
812 case 32000: sfreq_idx
= 2; break;
813 case 44100: sfreq_idx
= 0; break;
814 case 48000: sfreq_idx
= 1; break;
815 default : return; /* Just to avoid compiler warnings */
817 parm
= l3_parm
+ sfreq_idx
;
823 cbmax_l
= parm
->long_data
.cbmax_l
;
825 #if ORG_NUMLINES_NORM
826 for (i
= 0, j
= 0; i
< cbmax_l
; i
++)
828 numlines_l
[i
] = parm
->long_data
.numlines_l
[i
];
830 for (k
= 0; k
< numlines_l
[i
]; k
++)
831 partition_l
[j
++] = i
;
834 numlines_l
= parm
->long_data
.numlines_l
;
837 minval
= parm
->long_data
.minval
;
838 qthr_l
= parm
->long_data
.qthr_l
;
839 norm_l
= parm
->long_data
.norm_l
;
840 bval_l
= parm
->long_data
.bval_l
;
844 Compute the normed spreading function norm_l[i] * s3_l[i][j]
846 #if ORG_NUMLINES_NORM
847 for (i
= 0; i
< cbmax_l
; i
++)
849 double x
, temp
, tempx
, tempy
;
851 for (j
= 0; j
< cbmax_l
; j
++)
853 /* tempx = (bval_l[i]-bval_l[j]) * 1.05; */
855 tempx
= (bval_l
[i
]-bval_l
[j
]) * 3.0;
857 tempx
= (bval_l
[i
]-bval_l
[j
]) * 1.5;
858 /* if (j >= i) tempx = (bval_l[j]-bval_l[i]) * 3.0;
859 else tempx = (bval_l[j]-bval_l[i]) * 1.5; */
860 if (tempx
> 0.5 && tempx
< 2.5)
863 x
= 8.0 * temp
* (temp
-2.0);
867 tempy
= 15.811389 + 7.5*tempx
- 17.5*sqrt(1.0+tempx
*tempx
);
868 if (tempy
<= -60.0) s3_l
[i
][j
] = 0.0;
869 else s3_l
[i
][j
] = exp((x
+ tempy
) * LN_TO_LOG10
);
873 calc_normed_spreading (cbmax_l
, bval_l
, normed_s3_l
, lo_s3_l
, hi_s3_l
, norm_l
);
878 Read short block data
880 cbmax_s
= parm
->short_data
.cbmax_s
;
882 #if ORG_NUMLINES_NORM
883 for (i
= 0, j
= 0; i
< cbmax_s
; i
++)
885 numlines_l
[i
] = parm
->short_data
.numlines_s
[i
];
887 for (k
= 0; k
< numlines_l
[i
]; k
++)
888 partition_s
[j
++] = i
;
891 numlines_s
= parm
->short_data
.numlines_s
;
894 qthr_s
= parm
->short_data
.qthr_s
;
895 norm_s
= parm
->short_data
.norm_s
;
896 SNR_s
= parm
->short_data
.SNR_s
;
897 bval_s
= parm
->short_data
.bval_s
;
900 #if !ORG_NUMLINES_NORM
903 Compute the normed spreading function norm_s[i] * s3_s[i][j]
905 calc_normed_spreading (cbmax_s
, bval_s
, normed_s3_s
, lo_s3_s
, hi_s3_s
, norm_s
);
911 Read long block data for converting threshold
912 calculation partitions to scale factor bands
914 cbw_l
= parm
->long_thres
.cbw_l
;
915 bu_l
= parm
->long_thres
.bu_l
;
916 bo_l
= parm
->long_thres
.bo_l
;
917 w1_l
= parm
->long_thres
.w1_l
;
918 w2_l
= parm
->long_thres
.w2_l
;
922 Read short block data for converting threshold
923 calculation partitions to scale factor bands
925 cbw_s
= parm
->short_thres
.cbw_s
;
926 bu_s
= parm
->short_thres
.bu_s
;
927 bo_s
= parm
->short_thres
.bo_s
;
928 w1_s
= parm
->short_thres
.w1_s
;
929 w2_s
= parm
->short_thres
.w2_s
;
932 #else /* NEW_L3PARM_TABLES */
934 static void L3para_read
937 int numlines_l
[CBANDS
],
938 int partition_l
[HBLKSIZE
],
939 double minval
[CBANDS
],
940 double qthr_l
[CBANDS
],
941 double norm_l
[CBANDS
],
942 double s3_l
[CBANDS
][CBANDS
],
943 int partition_s
[HBLKSIZE_s
],
944 double qthr_s
[CBANDS_s
],
945 double norm_s
[CBANDS_s
],
946 double SNR_s
[CBANDS_s
],
950 double w1_l
[SBMAX_l
],
951 double w2_l
[SBMAX_l
],
955 double w1_s
[SBMAX_s
],
959 static double bval_l
[CBANDS
];
971 /* Read long block data */
975 case 32000: rpa1
= psy_longBlock_32000_58
; cbmax_tp
= 59; break;
976 case 44100: rpa1
= psy_longBlock_44100_62
; cbmax_tp
= 63; break;
977 case 48000: rpa1
= psy_longBlock_48000_61
; cbmax_tp
= 62; break;
978 default : return; /* Just to avoid compiler warnings */
981 for (i
= 0, k2
= 0; i
< cbmax_tp
; i
++)
983 numlines_l
[i
] = rpa1
->lines
;
984 minval
[i
] = rpa1
->minVal
;
985 qthr_l
[i
] = rpa1
->qthr
;
986 norm_l
[i
] = rpa1
->norm
;
987 bval_l
[i
] = rpa1
->bVal
;
990 for (k
= 0; k
< numlines_l
[i
]; k
++)
991 partition_l
[k2
++] = i
;
995 /************************************************************************
996 * Now compute the spreading function, s[j][i], the value of the spread-*
997 * ing function, centered at band j, for band i, store for later use *
998 ************************************************************************/
1000 for (i
= 0; i
< cbmax_tp
; i
++)
1002 double x
, temp
, tempx
, tempy
;
1004 for (j
= 0; j
< cbmax_tp
; j
++)
1006 /* tempx = (bval_l[i]-bval_l[j]) * 1.05; */
1008 tempx
= (bval_l
[i
]-bval_l
[j
]) * 3.0;
1010 tempx
= (bval_l
[i
]-bval_l
[j
]) * 1.5;
1011 /* if (j >= i) tempx = (bval_l[j]-bval_l[i]) * 3.0;
1012 else tempx = (bval_l[j]-bval_l[i]) * 1.5; */
1013 if (tempx
> 0.5 && tempx
< 2.5)
1016 x
= 8.0 * temp
* (temp
-2.0);
1020 tempy
= 15.811389 + 7.5*tempx
- 17.5*sqrt(1.0+tempx
*tempx
);
1021 if (tempy
<= -60.0) s3_l
[i
][j
] = 0.0;
1022 else s3_l
[i
][j
] = exp((x
+ tempy
) * LN_TO_LOG10
);
1027 /* Read short block data */
1031 case 32000: rpa2
= psy_shortBlock_32000_41
; cbmax_tp
= 42; break;
1032 case 44100: rpa2
= psy_shortBlock_44100_38
; cbmax_tp
= 39; break;
1033 case 48000: rpa2
= psy_shortBlock_48000_37
; cbmax_tp
= 38; break;
1034 default : return; /* Just to avoid compiler warnings */
1037 for (i
= 0, k2
= 0; i
< cbmax_tp
; i
++)
1039 numlines_l
[i
] = rpa2
->lines
;
1040 qthr_s
[i
] = rpa2
->qthr
;
1041 norm_s
[i
] = rpa2
->norm
;
1042 SNR_s
[i
] = rpa2
->snr
;
1045 for (k
= 0; k
< numlines_l
[i
]; k
++)
1046 partition_s
[k2
++] = i
;
1050 /* Read long block data for converting threshold calculation
1051 partitions to scale factor bands */
1055 case 32000: rpa3
= psy_data3_32000_20
; break;
1056 case 44100: rpa3
= psy_data3_44100_20
; break;
1057 case 48000: rpa3
= psy_data3_48000_20
; break;
1058 default : return; /* Just to avoid compiler warnings */
1062 for (i
= 0; i
< sbmax
; i
++)
1064 cbw_l
[i
] = rpa3
->cbw
;
1073 /* Read short block data for converting threshold calculation
1074 partitions to scale factor bands */
1078 case 32000: rpa3
= psy_data4_32000_11
; break;
1079 case 44100: rpa3
= psy_data4_44100_11
; break;
1080 case 48000: rpa3
= psy_data4_48000_11
; break;
1081 default : return; /* Just to avoid compiler warnings */
1085 for (i
= 0; i
< sbmax
; i
++)
1087 cbw_s
[i
] = rpa3
->cbw
;
1096 #endif /* NEW_L3PARM_TABLES */
1102 #if !ORG_NUMLINES_NORM && NEW_L3PARM_TABLES
1104 /* ======================================================================================== */
1105 /* calc_normed_spreading */
1106 /* ======================================================================================== */
1108 Compute the normed spreading function,
1109 the normed value of the spreading function,
1110 centered at band j, for band i, store for later use
1112 Since this is a band matrix, we store only the non-zero entries
1113 in linear order in the single dimension array normed_s3.
1115 The array has to be accessed in linear order, too, starting with line 0,
1116 up to line cbmax-1. For line b, the current entries represent
1118 norm[b] * s3[b][lo_s3[b]] ... norm[b] * s3[b][hi_s3[b]-1]
1120 Normally, we could easily compute the norm [building the reciprocal of the line sum].
1121 Alas, dist10 uses somewhat (strange and) different, that made our norm differring too
1122 much at the last few lines. Thus, we renounce and use the original values.
1125 static void calc_normed_spreading
1127 int cbmax
, /* number of lines and rows */
1128 const double bval
[], /* input values to compute the matrix */
1129 FLOAT s3_ptr
[], /* the resulting non-zero entries */
1142 for (i
= 0; i
< cbmax
; i
++)
1144 non_zero_part
= FALSE
;
1145 hi_s3
[i
] = cbmax
; /* we preset this value for the case that the line ends with a non-zero entry */
1147 for (j
= 0; j
< cbmax
; j
++)
1150 arg
= (bval
[i
] - bval
[j
]) * 3.0;
1152 arg
= (bval
[i
] - bval
[j
]) * 1.5;
1154 if (arg
> 0.5 && arg
< 2.5)
1155 x
= 8.0 * (arg
- 0.5) * (arg
- 2.5);
1161 y
= 15.811389 + 7.5 * arg
- 17.5 * sqrt(1.0 + arg
* arg
);
1165 if (non_zero_part
) /* only zeroes will follow */
1168 break; /* so cut the computing for this line */
1173 s3
[j
] = exp((x
+ y
) * LN_TO_LOG10
);
1175 if (! non_zero_part
)
1178 non_zero_part
= TRUE
; /* the first non-zero entry ends the non_zero_part */
1183 for (j
= lo_s3
[i
]; j
< hi_s3
[i
]; j
++)
1184 *s3_ptr
++ = s3
[j
] * norm
[i
];
1188 #endif /* ORG_NUMLINES_NORM */