removed "bool" occurence.
[swftools.git] / lib / bladeenc / l3psy.c
blob2b9960f0c27b84e2530b9a7ab3fb14a726071fc4
1 /*
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
27 2000-12-11 ap
29 - speed up: faster psycho_anal()
30 - optional bug fix: integrated better norm calclulation and block selecting
32 2000-12-12 ap
34 - use SHORT_FFT_MIN_IDX to allow switching of "ORG_SHORT_CW_LIMIT" in "l3psy.h"
36 2001-01-12 ap
38 - use some explicit type casting to avoid compiler warnings
41 #define RING_BUFFER 1
47 #include <stdlib.h>
49 #include "common.h"
50 #include "tables.h"
51 #include "encoder.h"
52 #include "l3side.h"
53 #include "l3psy.h"
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;
70 #if RING_BUFFER==1
71 static int savebuf_start_idx[2];
72 #endif
76 #if NEW_L3PARM_TABLES
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;
85 #if ORG_NUMLINES_NORM
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;
95 #else
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 */
112 #else
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 */
164 int lo_s3[],
165 int hi_s3[],
166 const double norm[]
168 #endif
170 #else
172 static void L3para_read
174 int sfreq,
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],
185 int cbw_l[SBMAX_l],
186 int bu_l[SBMAX_l],
187 int bo_l[SBMAX_l],
188 double w1_l[SBMAX_l],
189 double w2_l[SBMAX_l],
190 int cbw_s[SBMAX_s],
191 int bu_s[SBMAX_s],
192 int bo_s[SBMAX_s],
193 double w1_s[SBMAX_s],
194 double w2_s[SBMAX_s]
197 #endif
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++)
234 nb_1[ch][i] = 0;
235 nb_2[ch][i] = 0;
240 /* clear blocktype information */
242 for (ch = 0; ch < 2; ch++)
243 blocktype_old[ch] = NORM_TYPE;
246 sync_flush = 768;
247 flush = 576;
248 syncsize = 1344; /* sync_flush + flush */
251 #if RING_BUFFER==1
252 for (ch = 0; ch < 2; ch++)
253 savebuf_start_idx[ch] = 0;
254 #endif
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++)
274 r[ch][i][j] = 0.0;
275 phi_sav[ch][i][j] = 0.0;
281 #if NEW_L3PARM_TABLES
282 L3para_read ((int) sfreq);
283 #else
284 L3para_read
286 (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
292 #endif
295 /* Set unpredicatiblility of remaining spectral lines to 0.4 */
297 for (j = 206; j < HBLKSIZE; j++)
298 cw[j] = 0.4;
305 /*____ psycho_anal_exit() ___________________________________________________*/
307 void psycho_anal_exit( void )
309 /* nothing to do */
316 /*____ psycho_anal() ________________________________________________________*/
318 void psycho_anal
320 #if ORG_BUFFERS
321 short int *buffer,
322 short int savebuf[2048],
323 #else
324 FLOAT *buffer,
325 int buffer_idx,
326 #endif
327 int ch,
328 int lay,
329 /* float snr32[32], */
330 double ratio_d[SBMAX_l],
331 double ratio_ds[SBMAX_s][3],
332 double *pe,
333 gr_info *cod_info
336 int blocktype;
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
342 FLOAT *s3_ptr;
343 #endif
345 int sblock;
347 double thr [CBANDS];
348 double eb [CBANDS];
349 FLOAT cb [CBANDS];
350 FLOAT wsamp_r [HBLKSIZE];
351 FLOAT wsamp_i [HBLKSIZE];
353 FLOAT energy [HBLKSIZE];
354 FLOAT phi [6];
355 FLOAT energy_s [3][BLKSIZE_s];
356 FLOAT phi_s [3][52];
358 #if ORG_BUFFERS
359 #if RING_BUFFER==1
360 int beg, idx, fin;
361 #endif
362 #else
363 # define savebuf buffer
364 # define beg buffer_idx
365 int idx, fin;
366 #endif
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];
377 if (ch == 0)
378 old = 1 - (new_ = oldest = old);
381 #if ORG_BUFFERS
382 /**********************************************************************
383 * Delay signal by sync_flush=768 samples *
384 **********************************************************************/
386 # if RING_BUFFER==0
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++;
392 # else
393 beg = savebuf_start_idx[ch] = (savebuf_start_idx[ch] + flush) & 2047;
395 idx = (beg + sync_flush) & 2047;
396 fin = (idx + flush) & 2047;
397 if (idx >= fin)
399 while (idx < 2048)
400 savebuf[idx++] = *buffer++;
401 idx = 0;
403 while (idx < fin)
404 savebuf[idx++] = *buffer++;
405 # endif
406 #endif
409 /**********************************************************************
410 * compute unpredicatability of first six spectral lines *
411 **********************************************************************/
413 #if RING_BUFFER==0
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++];
419 #else
420 j = 0; k = 0;
421 idx = beg;
422 fin = (idx + BLKSIZE) & 2047;
423 if (idx >= fin)
425 while (idx < 2048)
427 wsamp_r[j] = window[k++] * savebuf[idx++];
428 wsamp_i[j] = window[k++] * savebuf[idx++];
429 j++;
431 idx = 0;
433 while (idx < fin)
435 wsamp_r[j] = window[k++] * savebuf[idx++];
436 wsamp_i[j] = window[k++] * savebuf[idx++];
437 j++;
439 #endif
441 fft(wsamp_r, wsamp_i, energy, phi, BLKSIZE); /* long FFT */
443 for (j = 0; j < 6; j++)
444 { /* calculate unpredictability measure cw */
445 double r1, phi1;
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);
452 if (temp3 != 0.0)
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;
458 else
459 cw[j] = 0;
463 /**********************************************************************
464 * compute unpredicatibility of next 200 spectral lines *
465 **********************************************************************/
467 for (b = 0; b < 3; b++)
469 #if RING_BUFFER==0
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++];
475 #else
476 j = 0; k = 0;
477 idx = (beg + 128*(2 + b)) & 2047;
478 fin = (idx + BLKSIZE_s) & 2047;
479 if (idx >= fin)
481 while (idx < 2048)
483 wsamp_r[j] = window_s[k++] * savebuf[idx++];
484 wsamp_i[j] = window_s[k++] * savebuf[idx++];
485 j++;
487 idx = 0;
489 while (idx < fin)
491 wsamp_r[j] = window_s[k++] * savebuf[idx++];
492 wsamp_i[j] = window_s[k++] * savebuf[idx++];
493 j++;
495 #endif
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 */
502 double r1, phi1;
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]);
507 phi1 = phi_s[1][k];
509 temp3 = r1 + fabs(r_prime);
510 if (temp3 != 0.0)
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;
516 else
517 cw[j] = 0.0;
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++)
532 eb[b] = 0.0;
533 cb[b] = 0.0;
535 for (j = 0; j < HBLKSIZE; j++)
537 int tp = partition_l[j];
538 if (tp >= 0)
540 eb[tp] += energy[j];
541 cb[tp] += cw[j] * energy[j];
545 #else
547 j = 0;
548 for (b = 0; b < cbmax_l; b++)
550 eb[b] = 0.0;
551 cb[b] = 0.0;
554 Calculate the energy and the unpredictability in the threshold
555 calculation partitions
557 cbmax_l holds the number of valid numlines_l entries
559 k = numlines_l[b];
560 do {
561 eb[b] += energy[j];
562 cb[b] += cw[j] * energy[j];
563 } while (j++, --k);
566 s3_ptr = normed_s3_l;
568 #endif
571 *pe = 0.0;
573 for (b = 0; b < cbmax_l; b++)
575 FLOAT nb;
576 FLOAT ecb = 0.0;
577 double ctb = 0.0;
578 double SNR_l;
579 double cbb, tbb;
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];
592 #else
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];
598 #endif
602 calculate the tonality of each threshold calculation partition
603 calculate the SNR in each threshhold calculation partition
605 if (ecb != 0.0)
607 cbb = ctb / ecb;
608 if (cbb < 0.01)
609 cbb = 0.01;
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 */
613 else
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);
622 #else
623 nb = ecb * exp(-SNR_l * LN_TO_LOG10); /* our ecb is already normed */
624 #endif
627 pre-echo control
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]);
631 nb_1[ch][b] = nb;
635 calculate percetual entropy
637 thr[b] -> thr[b]+1.0 : for non sound portition
639 if (eb[b] > thr[b])
640 *pe += numlines_l[b] * log((eb[b]+1.0) / (thr[b]+1.0));
644 #define switch_pe 1800
647 if (*pe < switch_pe)
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++)
661 int bu = bu_l[sfb];
662 int bo = bo_l[sfb];
663 double en = w1_l[sfb] * eb[bu] + w2_l[sfb] * eb[bo];
665 for (b = bu+1; b < bo; b++)
666 en += eb[b];
668 if (en != 0.0)
670 double thm = w1_l[sfb] * thr[bu] + w2_l[sfb] * thr[bo];
672 for (b = bu+1; b < bo; b++)
673 thm += thr[b];
675 ratio[ch][sfb] = thm / en;
677 else
678 ratio[ch][sfb] = 0.0;
681 else
683 /* attack : use short blocks */
684 blocktype = SHORT_TYPE;
685 #if ORG_BLOCK_SELECT
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;
690 #else /* ISO */
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;
695 #endif
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++)
705 eb[b] = 0.0;
707 for (j = 0; j < HBLKSIZE_s; j++)
708 eb[partition_s[j]] += energy_s[sblock][j];
710 #else
712 j = 0;
713 for (b = 0; b < cbmax_s; b++)
715 eb[b] = 0.0;
718 Calculate the energy and the unpredictability in the threshold
719 calculation partitions
721 cbmax_s holds the number of valid numlines_s entries
723 k = numlines_s[b];
724 do {
725 eb[b] += energy_s[sblock][j];
726 } while (j++, --k);
729 s3_ptr = normed_s3_s;
730 #endif
732 for (b = 0; b < cbmax_s; b++)
734 FLOAT nb;
735 FLOAT ecb = 0.0;
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);
742 #else
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 */
747 #endif
748 thr[b] = MAX(qthr_s[b], nb);
751 for (sfb = 0; sfb < SBMAX_s; sfb++)
753 int bu = bu_s[sfb];
754 int bo = bo_s[sfb];
755 double en = w1_s[sfb] * eb[bu] + w2_s[sfb] * eb[bo];
757 for (b = bu+1; b < bo; b++)
758 en += eb[b];
759 if (en != 0.0)
761 double thm = w1_s[sfb] * thr[bu] + w2_s[sfb] * thr[bo];
763 for (b = bu+1; b < bo; b++)
764 thm += thr[b];
766 ratio_s[ch][sfb][sblock] = thm / en;
768 else
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;
779 else
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)
795 int sfreq_idx;
796 l3_parm_block *parm;
797 double *bval_l, *bval_s;
799 #if ORG_NUMLINES_NORM
800 int cbmax_l, cbmax_s;
801 int i, j, k;
802 #else
803 double *norm_l, *norm_s;
804 #endif
808 Set parameter block
810 switch (sfreq)
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;
821 Read long block data
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;
833 #else
834 numlines_l = parm->long_data.numlines_l;
835 #endif
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; */
854 if (j >= i)
855 tempx = (bval_l[i]-bval_l[j]) * 3.0;
856 else
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)
862 temp = tempx - 0.5;
863 x = 8.0 * temp * (temp-2.0);
865 else x = 0.0;
866 tempx += 0.474;
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);
872 #else
873 calc_normed_spreading (cbmax_l, bval_l, normed_s3_l, lo_s3_l, hi_s3_l, norm_l);
874 #endif
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;
890 #else
891 numlines_s = parm->short_data.numlines_s;
892 #endif
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);
907 #endif
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
936 int sfreq,
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],
947 int cbw_l[SBMAX_l],
948 int bu_l[SBMAX_l],
949 int bo_l[SBMAX_l],
950 double w1_l[SBMAX_l],
951 double w2_l[SBMAX_l],
952 int cbw_s[SBMAX_s],
953 int bu_s[SBMAX_s],
954 int bo_s[SBMAX_s],
955 double w1_s[SBMAX_s],
956 double w2_s[SBMAX_s]
959 static double bval_l[CBANDS];
960 int cbmax_tp;
962 int sbmax;
963 int i, j, k, k2;
966 psyDataElem *rpa1;
967 psyDataElem2 *rpa2;
968 psyDataElem3 *rpa3;
971 /* Read long block data */
973 switch (sfreq)
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;
988 rpa1++;
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; */
1007 if (j >= i)
1008 tempx = (bval_l[i]-bval_l[j]) * 3.0;
1009 else
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)
1015 temp = tempx - 0.5;
1016 x = 8.0 * temp * (temp-2.0);
1018 else x = 0.0;
1019 tempx += 0.474;
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 */
1029 switch (sfreq)
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;
1043 rpa2++;
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 */
1053 switch (sfreq)
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 */
1060 sbmax = SBMAX_l;
1062 for (i = 0; i < sbmax; i++)
1064 cbw_l[i] = rpa3->cbw;
1065 bu_l[i] = rpa3->bu;
1066 bo_l[i] = rpa3->bo;
1067 w1_l[i] = rpa3->w1;
1068 w2_l[i] = rpa3->w2;
1069 rpa3++;
1073 /* Read short block data for converting threshold calculation
1074 partitions to scale factor bands */
1076 switch (sfreq)
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 */
1083 sbmax = SBMAX_s;
1085 for (i = 0; i < sbmax; i++)
1087 cbw_s[i] = rpa3->cbw;
1088 bu_s[i] = rpa3->bu;
1089 bo_s[i] = rpa3->bo;
1090 w1_s[i] = rpa3->w1;
1091 w2_s[i] = rpa3->w2;
1092 rpa3++;
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 */
1130 int lo_s3[],
1131 int hi_s3[],
1132 const double norm[]
1135 double arg, x, y;
1136 double s3[CBANDS];
1137 int i, j;
1138 int non_zero_part;
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++)
1149 if (j >= i)
1150 arg = (bval[i] - bval[j]) * 3.0;
1151 else
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);
1156 else
1157 x = 0.0;
1159 arg += 0.474;
1161 y = 15.811389 + 7.5 * arg - 17.5 * sqrt(1.0 + arg * arg);
1163 if (y <= -60.0)
1165 if (non_zero_part) /* only zeroes will follow */
1167 hi_s3[i] = j;
1168 break; /* so cut the computing for this line */
1171 else
1173 s3[j] = exp((x + y) * LN_TO_LOG10);
1175 if (! non_zero_part)
1177 lo_s3[i] = j;
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 */