forked from BB9z/LAME-xcframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsymodel.c
2167 lines (1933 loc) · 69.3 KB
/
psymodel.c
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* psymodel.c
*
* Copyright (c) 1999-2000 Mark Taylor
* Copyright (c) 2001-2002 Naoki Shibata
* Copyright (c) 2000-2003 Takehiro Tominaga
* Copyright (c) 2000-2012 Robert Hegemann
* Copyright (c) 2000-2005 Gabriel Bouvigne
* Copyright (c) 2000-2005 Alexander Leidinger
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library 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: psymodel.c,v 1.216 2017/09/06 19:38:23 aleidinger Exp $ */
/*
PSYCHO ACOUSTICS
This routine computes the psycho acoustics, delayed by one granule.
Input: buffer of PCM data (1024 samples).
This window should be centered over the 576 sample granule window.
The routine will compute the psycho acoustics for
this granule, but return the psycho acoustics computed
for the *previous* granule. This is because the block
type of the previous granule can only be determined
after we have computed the psycho acoustics for the following
granule.
Output: maskings and energies for each scalefactor band.
block type, PE, and some correlation measures.
The PE is used by CBR modes to determine if extra bits
from the bit reservoir should be used. The correlation
measures are used to determine mid/side or regular stereo.
*/
/*
Notation:
barks: a non-linear frequency scale. Mapping from frequency to
barks is given by freq2bark()
scalefactor bands: The spectrum (frequencies) are broken into
SBMAX "scalefactor bands". Thes bands
are determined by the MPEG ISO spec. In
the noise shaping/quantization code, we allocate
bits among the partition bands to achieve the
best possible quality
partition bands: The spectrum is also broken into about
64 "partition bands". Each partition
band is about .34 barks wide. There are about 2-5
partition bands for each scalefactor band.
LAME computes all psycho acoustic information for each partition
band. Then at the end of the computations, this information
is mapped to scalefactor bands. The energy in each scalefactor
band is taken as the sum of the energy in all partition bands
which overlap the scalefactor band. The maskings can be computed
in the same way (and thus represent the average masking in that band)
or by taking the minmum value multiplied by the number of
partition bands used (which represents a minimum masking in that band).
*/
/*
The general outline is as follows:
1. compute the energy in each partition band
2. compute the tonality in each partition band
3. compute the strength of each partion band "masker"
4. compute the masking (via the spreading function applied to each masker)
5. Modifications for mid/side masking.
Each partition band is considiered a "masker". The strength
of the i'th masker in band j is given by:
s3(bark(i)-bark(j))*strength(i)
The strength of the masker is a function of the energy and tonality.
The more tonal, the less masking. LAME uses a simple linear formula
(controlled by NMT and TMN) which says the strength is given by the
energy divided by a linear function of the tonality.
*/
/*
s3() is the "spreading function". It is given by a formula
determined via listening tests.
The total masking in the j'th partition band is the sum over
all maskings i. It is thus given by the convolution of
the strength with s3(), the "spreading function."
masking(j) = sum_over_i s3(i-j)*strength(i) = s3 o strength
where "o" = convolution operator. s3 is given by a formula determined
via listening tests. It is normalized so that s3 o 1 = 1.
Note: instead of a simple convolution, LAME also has the
option of using "additive masking"
The most critical part is step 2, computing the tonality of each
partition band. LAME has two tonality estimators. The first
is based on the ISO spec, and measures how predictiable the
signal is over time. The more predictable, the more tonal.
The second measure is based on looking at the spectrum of
a single granule. The more peaky the spectrum, the more
tonal. By most indications, the latter approach is better.
Finally, in step 5, the maskings for the mid and side
channel are possibly increased. Under certain circumstances,
noise in the mid & side channels is assumed to also
be masked by strong maskers in the L or R channels.
Other data computed by the psy-model:
ms_ratio side-channel / mid-channel masking ratio (for previous granule)
ms_ratio_next side-channel / mid-channel masking ratio for this granule
percep_entropy[2] L and R values (prev granule) of PE - A measure of how
much pre-echo is in the previous granule
percep_entropy_MS[2] mid and side channel values (prev granule) of percep_entropy
energy[4] L,R,M,S energy in each channel, prev granule
blocktype_d[2] block type to use for previous granule
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <float.h>
#include "lame.h"
#include "machine.h"
#include "encoder.h"
#include "util.h"
#include "psymodel.h"
#include "lame_global_flags.h"
#include "fft.h"
#include "lame-analysis.h"
#define NSFIRLEN 21
#ifdef M_LN10
#define LN_TO_LOG10 (M_LN10/10)
#else
#define LN_TO_LOG10 0.2302585093
#endif
/*
L3psycho_anal. Compute psycho acoustics.
Data returned to the calling program must be delayed by one
granule.
This is done in two places.
If we do not need to know the blocktype, the copying
can be done here at the top of the program: we copy the data for
the last granule (computed during the last call) before it is
overwritten with the new data. It looks like this:
0. static psymodel_data
1. calling_program_data = psymodel_data
2. compute psymodel_data
For data which needs to know the blocktype, the copying must be
done at the end of this loop, and the old values must be saved:
0. static psymodel_data_old
1. compute psymodel_data
2. compute possible block type of this granule
3. compute final block type of previous granule based on #2.
4. calling_program_data = psymodel_data_old
5. psymodel_data_old = psymodel_data
*/
/* psycho_loudness_approx
jd - 2001 mar 12
in: energy - BLKSIZE/2 elements of frequency magnitudes ^ 2
gfp - uses out_samplerate, ATHtype (also needed for ATHformula)
returns: loudness^2 approximation, a positive value roughly tuned for a value
of 1.0 for signals near clipping.
notes: When calibrated, feeding this function binary white noise at sample
values +32767 or -32768 should return values that approach 3.
ATHformula is used to approximate an equal loudness curve.
future: Data indicates that the shape of the equal loudness curve varies
with intensity. This function might be improved by using an equal
loudness curve shaped for typical playback levels (instead of the
ATH, that is shaped for the threshold). A flexible realization might
simply bend the existing ATH curve to achieve the desired shape.
However, the potential gain may not be enough to justify an effort.
*/
static FLOAT
psycho_loudness_approx(FLOAT const *energy, FLOAT const *eql_w)
{
int i;
FLOAT loudness_power;
loudness_power = 0.0;
/* apply weights to power in freq. bands */
for (i = 0; i < BLKSIZE / 2; ++i)
loudness_power += energy[i] * eql_w[i];
loudness_power *= VO_SCALE;
return loudness_power;
}
/* mask_add optimization */
/* init the limit values used to avoid computing log in mask_add when it is not necessary */
/* For example, with i = 10*log10(m2/m1)/10*16 (= log10(m2/m1)*16)
*
* abs(i)>8 is equivalent (as i is an integer) to
* abs(i)>=9
* i>=9 || i<=-9
* equivalent to (as i is the biggest integer smaller than log10(m2/m1)*16
* or the smallest integer bigger than log10(m2/m1)*16 depending on the sign of log10(m2/m1)*16)
* log10(m2/m1)>=9/16 || log10(m2/m1)<=-9/16
* exp10 is strictly increasing thus this is equivalent to
* m2/m1 >= 10^(9/16) || m2/m1<=10^(-9/16) which are comparisons to constants
*/
#define I1LIMIT 8 /* as in if(i>8) */
#define I2LIMIT 23 /* as in if(i>24) -> changed 23 */
#define MLIMIT 15 /* as in if(m<15) */
/* pow(10, (I1LIMIT + 1) / 16.0); */
static const FLOAT ma_max_i1 = 3.6517412725483771;
/* pow(10, (I2LIMIT + 1) / 16.0); */
static const FLOAT ma_max_i2 = 31.622776601683793;
/* pow(10, (MLIMIT) / 10.0); */
static const FLOAT ma_max_m = 31.622776601683793;
/*This is the masking table:
According to tonality, values are going from 0dB (TMN)
to 9.3dB (NMT).
After additive masking computation, 8dB are added, so
final values are going from 8dB to 17.3dB
*/
static const FLOAT tab[] = {
1.0 /*pow(10, -0) */ ,
0.79433 /*pow(10, -0.1) */ ,
0.63096 /*pow(10, -0.2) */ ,
0.63096 /*pow(10, -0.2) */ ,
0.63096 /*pow(10, -0.2) */ ,
0.63096 /*pow(10, -0.2) */ ,
0.63096 /*pow(10, -0.2) */ ,
0.25119 /*pow(10, -0.6) */ ,
0.11749 /*pow(10, -0.93) */
};
static const int tab_mask_add_delta[] = { 2, 2, 2, 1, 1, 1, 0, 0, -1 };
#define STATIC_ASSERT_EQUAL_DIMENSION(A,B) enum{static_assert_##A=1/((dimension_of(A) == dimension_of(B))?1:0)}
inline static int
mask_add_delta(int i)
{
STATIC_ASSERT_EQUAL_DIMENSION(tab_mask_add_delta,tab);
assert(i < (int)dimension_of(tab));
return tab_mask_add_delta[i];
}
static void
init_mask_add_max_values(void)
{
#ifndef NDEBUG
FLOAT const _ma_max_i1 = pow(10, (I1LIMIT + 1) / 16.0);
FLOAT const _ma_max_i2 = pow(10, (I2LIMIT + 1) / 16.0);
FLOAT const _ma_max_m = pow(10, (MLIMIT) / 10.0);
assert(fabs(ma_max_i1 - _ma_max_i1) <= FLT_EPSILON);
assert(fabs(ma_max_i2 - _ma_max_i2) <= FLT_EPSILON);
assert(fabs(ma_max_m - _ma_max_m ) <= FLT_EPSILON);
#endif
}
/* addition of simultaneous masking Naoki Shibata 2000/7 */
inline static FLOAT
vbrpsy_mask_add(FLOAT m1, FLOAT m2, int b, int delta)
{
static const FLOAT table2[] = {
1.33352 * 1.33352, 1.35879 * 1.35879, 1.38454 * 1.38454, 1.39497 * 1.39497,
1.40548 * 1.40548, 1.3537 * 1.3537, 1.30382 * 1.30382, 1.22321 * 1.22321,
1.14758 * 1.14758,
1
};
FLOAT ratio;
if (m1 < 0) {
m1 = 0;
}
if (m2 < 0) {
m2 = 0;
}
if (m1 <= 0) {
return m2;
}
if (m2 <= 0) {
return m1;
}
if (m2 > m1) {
ratio = m2 / m1;
}
else {
ratio = m1 / m2;
}
if (abs(b) <= delta) { /* approximately, 1 bark = 3 partitions */
/* originally 'if(i > 8)' */
if (ratio >= ma_max_i1) {
return m1 + m2;
}
else {
int i = (int) (FAST_LOG10_X(ratio, 16.0f));
return (m1 + m2) * table2[i];
}
}
if (ratio < ma_max_i2) {
return m1 + m2;
}
if (m1 < m2) {
m1 = m2;
}
return m1;
}
/* short block threshold calculation (part 2)
partition band bo_s[sfb] is at the transition from scalefactor
band sfb to the next one sfb+1; enn and thmm have to be split
between them
*/
static void
convert_partition2scalefac(PsyConst_CB2SB_t const *const gd, FLOAT const *eb, FLOAT const *thr,
FLOAT enn_out[], FLOAT thm_out[])
{
FLOAT enn, thmm;
int sb, b, n = gd->n_sb;
enn = thmm = 0.0f;
for (sb = b = 0; sb < n; ++b, ++sb) {
int const bo_sb = gd->bo[sb];
int const npart = gd->npart;
int const b_lim = bo_sb < npart ? bo_sb : npart;
while (b < b_lim) {
assert(eb[b] >= 0); /* iff failed, it may indicate some index error elsewhere */
assert(thr[b] >= 0);
enn += eb[b];
thmm += thr[b];
b++;
}
if (b >= npart) {
enn_out[sb] = enn;
thm_out[sb] = thmm;
++sb;
break;
}
assert(eb[b] >= 0); /* iff failed, it may indicate some index error elsewhere */
assert(thr[b] >= 0);
{
/* at transition sfb -> sfb+1 */
FLOAT const w_curr = gd->bo_weight[sb];
FLOAT const w_next = 1.0f - w_curr;
enn += w_curr * eb[b];
thmm += w_curr * thr[b];
enn_out[sb] = enn;
thm_out[sb] = thmm;
enn = w_next * eb[b];
thmm = w_next * thr[b];
}
}
/* zero initialize the rest */
for (; sb < n; ++sb) {
enn_out[sb] = 0;
thm_out[sb] = 0;
}
}
static void
convert_partition2scalefac_s(lame_internal_flags * gfc, FLOAT const *eb, FLOAT const *thr, int chn,
int sblock)
{
PsyStateVar_t *const psv = &gfc->sv_psy;
PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->s;
FLOAT enn[SBMAX_s], thm[SBMAX_s];
int sb;
convert_partition2scalefac(gds, eb, thr, enn, thm);
for (sb = 0; sb < SBMAX_s; ++sb) {
psv->en[chn].s[sb][sblock] = enn[sb];
psv->thm[chn].s[sb][sblock] = thm[sb];
}
}
/* longblock threshold calculation (part 2) */
static void
convert_partition2scalefac_l(lame_internal_flags * gfc, FLOAT const *eb, FLOAT const *thr, int chn)
{
PsyStateVar_t *const psv = &gfc->sv_psy;
PsyConst_CB2SB_t const *const gdl = &gfc->cd_psy->l;
FLOAT *enn = &psv->en[chn].l[0];
FLOAT *thm = &psv->thm[chn].l[0];
convert_partition2scalefac(gdl, eb, thr, enn, thm);
}
static void
convert_partition2scalefac_l_to_s(lame_internal_flags * gfc, FLOAT const *eb, FLOAT const *thr,
int chn)
{
PsyStateVar_t *const psv = &gfc->sv_psy;
PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->l_to_s;
FLOAT enn[SBMAX_s], thm[SBMAX_s];
int sb, sblock;
convert_partition2scalefac(gds, eb, thr, enn, thm);
for (sb = 0; sb < SBMAX_s; ++sb) {
FLOAT const scale = 1. / 64.f;
FLOAT const tmp_enn = enn[sb];
FLOAT const tmp_thm = thm[sb] * scale;
for (sblock = 0; sblock < 3; ++sblock) {
psv->en[chn].s[sb][sblock] = tmp_enn;
psv->thm[chn].s[sb][sblock] = tmp_thm;
}
}
}
static inline FLOAT
NS_INTERP(FLOAT x, FLOAT y, FLOAT r)
{
/* was pow((x),(r))*pow((y),1-(r)) */
if (r >= 1.0f)
return x; /* 99.7% of the time */
if (r <= 0.0f)
return y;
if (y > 0.0f)
return powf(x / y, r) * y; /* rest of the time */
return 0.0f; /* never happens */
}
static FLOAT
pecalc_s(III_psy_ratio const *mr, FLOAT masking_lower)
{
FLOAT pe_s;
static const FLOAT regcoef_s[] = {
11.8, /* these values are tuned only for 44.1kHz... */
13.6,
17.2,
32,
46.5,
51.3,
57.5,
67.1,
71.5,
84.6,
97.6,
130,
/* 255.8 */
};
unsigned int sb, sblock;
pe_s = 1236.28f / 4;
for (sb = 0; sb < SBMAX_s - 1; sb++) {
for (sblock = 0; sblock < 3; sblock++) {
FLOAT const thm = mr->thm.s[sb][sblock];
assert(sb < dimension_of(regcoef_s));
if (thm > 0.0f) {
FLOAT const x = thm * masking_lower;
FLOAT const en = mr->en.s[sb][sblock];
if (en > x) {
if (en > x * 1e10f) {
pe_s += regcoef_s[sb] * (10.0f * LOG10);
}
else {
assert(x > 0);
pe_s += regcoef_s[sb] * FAST_LOG10(en / x);
}
}
}
}
}
return pe_s;
}
static FLOAT
pecalc_l(III_psy_ratio const *mr, FLOAT masking_lower)
{
FLOAT pe_l;
static const FLOAT regcoef_l[] = {
6.8, /* these values are tuned only for 44.1kHz... */
5.8,
5.8,
6.4,
6.5,
9.9,
12.1,
14.4,
15,
18.9,
21.6,
26.9,
34.2,
40.2,
46.8,
56.5,
60.7,
73.9,
85.7,
93.4,
126.1,
/* 241.3 */
};
unsigned int sb;
pe_l = 1124.23f / 4;
for (sb = 0; sb < SBMAX_l - 1; sb++) {
FLOAT const thm = mr->thm.l[sb];
assert(sb < dimension_of(regcoef_l));
if (thm > 0.0f) {
FLOAT const x = thm * masking_lower;
FLOAT const en = mr->en.l[sb];
if (en > x) {
if (en > x * 1e10f) {
pe_l += regcoef_l[sb] * (10.0f * LOG10);
}
else {
assert(x > 0);
pe_l += regcoef_l[sb] * FAST_LOG10(en / x);
}
}
}
}
return pe_l;
}
static void
calc_energy(PsyConst_CB2SB_t const *l, FLOAT const *fftenergy, FLOAT * eb, FLOAT * max, FLOAT * avg)
{
int b, j;
for (b = j = 0; b < l->npart; ++b) {
FLOAT ebb = 0, m = 0;
int i;
for (i = 0; i < l->numlines[b]; ++i, ++j) {
FLOAT const el = fftenergy[j];
assert(el >= 0);
ebb += el;
if (m < el)
m = el;
}
eb[b] = ebb;
max[b] = m;
avg[b] = ebb * l->rnumlines[b];
assert(l->rnumlines[b] >= 0);
assert(ebb >= 0);
assert(eb[b] >= 0);
assert(max[b] >= 0);
assert(avg[b] >= 0);
}
}
static void
calc_mask_index_l(lame_internal_flags const *gfc, FLOAT const *max,
FLOAT const *avg, unsigned char *mask_idx)
{
PsyConst_CB2SB_t const *const gdl = &gfc->cd_psy->l;
FLOAT m, a;
int b, k;
int const last_tab_entry = sizeof(tab) / sizeof(tab[0]) - 1;
b = 0;
a = avg[b] + avg[b + 1];
assert(a >= 0);
if (a > 0.0f) {
m = max[b];
if (m < max[b + 1])
m = max[b + 1];
assert((gdl->numlines[b] + gdl->numlines[b + 1] - 1) > 0);
a = 20.0f * (m * 2.0f - a)
/ (a * (gdl->numlines[b] + gdl->numlines[b + 1] - 1));
k = (int) a;
if (k > last_tab_entry)
k = last_tab_entry;
mask_idx[b] = k;
}
else {
mask_idx[b] = 0;
}
for (b = 1; b < gdl->npart - 1; b++) {
a = avg[b - 1] + avg[b] + avg[b + 1];
assert(a >= 0);
if (a > 0.0f) {
m = max[b - 1];
if (m < max[b])
m = max[b];
if (m < max[b + 1])
m = max[b + 1];
assert((gdl->numlines[b - 1] + gdl->numlines[b] + gdl->numlines[b + 1] - 1) > 0);
a = 20.0f * (m * 3.0f - a)
/ (a * (gdl->numlines[b - 1] + gdl->numlines[b] + gdl->numlines[b + 1] - 1));
k = (int) a;
if (k > last_tab_entry)
k = last_tab_entry;
mask_idx[b] = k;
}
else {
mask_idx[b] = 0;
}
}
assert(b > 0);
assert(b == gdl->npart - 1);
a = avg[b - 1] + avg[b];
assert(a >= 0);
if (a > 0.0f) {
m = max[b - 1];
if (m < max[b])
m = max[b];
assert((gdl->numlines[b - 1] + gdl->numlines[b] - 1) > 0);
a = 20.0f * (m * 2.0f - a)
/ (a * (gdl->numlines[b - 1] + gdl->numlines[b] - 1));
k = (int) a;
if (k > last_tab_entry)
k = last_tab_entry;
mask_idx[b] = k;
}
else {
mask_idx[b] = 0;
}
assert(b == (gdl->npart - 1));
}
static void
vbrpsy_compute_fft_l(lame_internal_flags * gfc, const sample_t * const buffer[2], int chn,
int gr_out, FLOAT fftenergy[HBLKSIZE], FLOAT(*wsamp_l)[BLKSIZE])
{
SessionConfig_t const *const cfg = &gfc->cfg;
PsyStateVar_t *psv = &gfc->sv_psy;
plotting_data *plt = cfg->analysis ? gfc->pinfo : 0;
int j;
if (chn < 2) {
fft_long(gfc, *wsamp_l, chn, buffer);
}
else if (chn == 2) {
FLOAT const sqrt2_half = SQRT2 * 0.5f;
/* FFT data for mid and side channel is derived from L & R */
for (j = BLKSIZE - 1; j >= 0; --j) {
FLOAT const l = wsamp_l[0][j];
FLOAT const r = wsamp_l[1][j];
wsamp_l[0][j] = (l + r) * sqrt2_half;
wsamp_l[1][j] = (l - r) * sqrt2_half;
}
}
/*********************************************************************
* compute energies
*********************************************************************/
fftenergy[0] = wsamp_l[0][0];
fftenergy[0] *= fftenergy[0];
for (j = BLKSIZE / 2 - 1; j >= 0; --j) {
FLOAT const re = (*wsamp_l)[BLKSIZE / 2 - j];
FLOAT const im = (*wsamp_l)[BLKSIZE / 2 + j];
fftenergy[BLKSIZE / 2 - j] = (re * re + im * im) * 0.5f;
}
/* total energy */
{
FLOAT totalenergy = 0.0f;
for (j = 11; j < HBLKSIZE; j++)
totalenergy += fftenergy[j];
psv->tot_ener[chn] = totalenergy;
}
if (plt) {
for (j = 0; j < HBLKSIZE; j++) {
plt->energy[gr_out][chn][j] = plt->energy_save[chn][j];
plt->energy_save[chn][j] = fftenergy[j];
}
}
}
static void
vbrpsy_compute_fft_s(lame_internal_flags const *gfc, const sample_t * const buffer[2], int chn,
int sblock, FLOAT(*fftenergy_s)[HBLKSIZE_s], FLOAT(*wsamp_s)[3][BLKSIZE_s])
{
int j;
if (sblock == 0 && chn < 2) {
fft_short(gfc, *wsamp_s, chn, buffer);
}
if (chn == 2) {
FLOAT const sqrt2_half = SQRT2 * 0.5f;
/* FFT data for mid and side channel is derived from L & R */
for (j = BLKSIZE_s - 1; j >= 0; --j) {
FLOAT const l = wsamp_s[0][sblock][j];
FLOAT const r = wsamp_s[1][sblock][j];
wsamp_s[0][sblock][j] = (l + r) * sqrt2_half;
wsamp_s[1][sblock][j] = (l - r) * sqrt2_half;
}
}
/*********************************************************************
* compute energies
*********************************************************************/
fftenergy_s[sblock][0] = (*wsamp_s)[sblock][0];
fftenergy_s[sblock][0] *= fftenergy_s[sblock][0];
for (j = BLKSIZE_s / 2 - 1; j >= 0; --j) {
FLOAT const re = (*wsamp_s)[sblock][BLKSIZE_s / 2 - j];
FLOAT const im = (*wsamp_s)[sblock][BLKSIZE_s / 2 + j];
fftenergy_s[sblock][BLKSIZE_s / 2 - j] = (re * re + im * im) * 0.5f;
}
}
/*********************************************************************
* compute loudness approximation (used for ATH auto-level adjustment)
*********************************************************************/
static void
vbrpsy_compute_loudness_approximation_l(lame_internal_flags * gfc, int gr_out, int chn,
const FLOAT fftenergy[HBLKSIZE])
{
PsyStateVar_t *psv = &gfc->sv_psy;
if (chn < 2) { /*no loudness for mid/side ch */
gfc->ov_psy.loudness_sq[gr_out][chn] = psv->loudness_sq_save[chn];
psv->loudness_sq_save[chn] = psycho_loudness_approx(fftenergy, gfc->ATH->eql_w);
}
}
/**********************************************************************
* Apply HPF of fs/4 to the input signal.
* This is used for attack detection / handling.
**********************************************************************/
static void
vbrpsy_attack_detection(lame_internal_flags * gfc, const sample_t * const buffer[2], int gr_out,
III_psy_ratio masking_ratio[2][2], III_psy_ratio masking_MS_ratio[2][2],
FLOAT energy[4], FLOAT sub_short_factor[4][3], int ns_attacks[4][4],
int uselongblock[2])
{
FLOAT ns_hpfsmpl[2][576];
SessionConfig_t const *const cfg = &gfc->cfg;
PsyStateVar_t *const psv = &gfc->sv_psy;
plotting_data *plt = cfg->analysis ? gfc->pinfo : 0;
int const n_chn_out = cfg->channels_out;
/* chn=2 and 3 = Mid and Side channels */
int const n_chn_psy = (cfg->mode == JOINT_STEREO) ? 4 : n_chn_out;
int chn, i, j;
memset(&ns_hpfsmpl[0][0], 0, sizeof(ns_hpfsmpl));
/* Don't copy the input buffer into a temporary buffer */
/* unroll the loop 2 times */
for (chn = 0; chn < n_chn_out; chn++) {
static const FLOAT fircoef[] = {
-8.65163e-18 * 2, -0.00851586 * 2, -6.74764e-18 * 2, 0.0209036 * 2,
-3.36639e-17 * 2, -0.0438162 * 2, -1.54175e-17 * 2, 0.0931738 * 2,
-5.52212e-17 * 2, -0.313819 * 2
};
/* apply high pass filter of fs/4 */
const sample_t *const firbuf = &buffer[chn][576 - 350 - NSFIRLEN + 192];
assert(dimension_of(fircoef) == ((NSFIRLEN - 1) / 2));
for (i = 0; i < 576; i++) {
FLOAT sum1, sum2;
sum1 = firbuf[i + 10];
sum2 = 0.0;
for (j = 0; j < ((NSFIRLEN - 1) / 2) - 1; j += 2) {
sum1 += fircoef[j] * (firbuf[i + j] + firbuf[i + NSFIRLEN - j]);
sum2 += fircoef[j + 1] * (firbuf[i + j + 1] + firbuf[i + NSFIRLEN - j - 1]);
}
ns_hpfsmpl[chn][i] = sum1 + sum2;
}
masking_ratio[gr_out][chn].en = psv->en[chn];
masking_ratio[gr_out][chn].thm = psv->thm[chn];
if (n_chn_psy > 2) {
/* MS maskings */
/*percep_MS_entropy [chn-2] = gfc -> pe [chn]; */
masking_MS_ratio[gr_out][chn].en = psv->en[chn + 2];
masking_MS_ratio[gr_out][chn].thm = psv->thm[chn + 2];
}
}
for (chn = 0; chn < n_chn_psy; chn++) {
FLOAT attack_intensity[12];
FLOAT en_subshort[12];
FLOAT en_short[4] = { 0, 0, 0, 0 };
FLOAT const *pf = ns_hpfsmpl[chn & 1];
int ns_uselongblock = 1;
if (chn == 2) {
for (i = 0, j = 576; j > 0; ++i, --j) {
FLOAT const l = ns_hpfsmpl[0][i];
FLOAT const r = ns_hpfsmpl[1][i];
ns_hpfsmpl[0][i] = l + r;
ns_hpfsmpl[1][i] = l - r;
}
}
/***************************************************************
* determine the block type (window type)
***************************************************************/
/* calculate energies of each sub-shortblocks */
for (i = 0; i < 3; i++) {
en_subshort[i] = psv->last_en_subshort[chn][i + 6];
assert(psv->last_en_subshort[chn][i + 4] > 0);
attack_intensity[i] = en_subshort[i] / psv->last_en_subshort[chn][i + 4];
en_short[0] += en_subshort[i];
}
for (i = 0; i < 9; i++) {
FLOAT const *const pfe = pf + 576 / 9;
FLOAT p = 1.;
for (; pf < pfe; pf++)
if (p < fabs(*pf))
p = fabs(*pf);
psv->last_en_subshort[chn][i] = en_subshort[i + 3] = p;
en_short[1 + i / 3] += p;
if (p > en_subshort[i + 3 - 2]) {
assert(en_subshort[i + 3 - 2] > 0);
p = p / en_subshort[i + 3 - 2];
}
else if (en_subshort[i + 3 - 2] > p * 10.0f) {
assert(p > 0);
p = en_subshort[i + 3 - 2] / (p * 10.0f);
}
else {
p = 0.0;
}
attack_intensity[i + 3] = p;
}
/* pulse like signal detection for fatboy.wav and so on */
for (i = 0; i < 3; ++i) {
FLOAT const enn =
en_subshort[i * 3 + 3] + en_subshort[i * 3 + 4] + en_subshort[i * 3 + 5];
FLOAT factor = 1.f;
if (en_subshort[i * 3 + 5] * 6 < enn) {
factor *= 0.5f;
if (en_subshort[i * 3 + 4] * 6 < enn) {
factor *= 0.5f;
}
}
sub_short_factor[chn][i] = factor;
}
if (plt) {
FLOAT x = attack_intensity[0];
for (i = 1; i < 12; i++) {
if (x < attack_intensity[i]) {
x = attack_intensity[i];
}
}
plt->ers[gr_out][chn] = plt->ers_save[chn];
plt->ers_save[chn] = x;
}
/* compare energies between sub-shortblocks */
{
FLOAT x = gfc->cd_psy->attack_threshold[chn];
for (i = 0; i < 12; i++) {
if (ns_attacks[chn][i / 3] == 0) {
if (attack_intensity[i] > x) {
ns_attacks[chn][i / 3] = (i % 3) + 1;
}
}
}
}
/* should have energy change between short blocks, in order to avoid periodic signals */
/* Good samples to show the effect are Trumpet test songs */
/* GB: tuned (1) to avoid too many short blocks for test sample TRUMPET */
/* RH: tuned (2) to let enough short blocks through for test sample FSOL and SNAPS */
for (i = 1; i < 4; i++) {
FLOAT const u = en_short[i - 1];
FLOAT const v = en_short[i];
FLOAT const m = Max(u, v);
if (m < 40000) { /* (2) */
if (u < 1.7f * v && v < 1.7f * u) { /* (1) */
if (i == 1 && ns_attacks[chn][0] <= ns_attacks[chn][i]) {
ns_attacks[chn][0] = 0;
}
ns_attacks[chn][i] = 0;
}
}
}
if (ns_attacks[chn][0] <= psv->last_attacks[chn]) {
ns_attacks[chn][0] = 0;
}
if (psv->last_attacks[chn] == 3 ||
ns_attacks[chn][0] + ns_attacks[chn][1] + ns_attacks[chn][2] + ns_attacks[chn][3]) {
ns_uselongblock = 0;
if (ns_attacks[chn][1] && ns_attacks[chn][0]) {
ns_attacks[chn][1] = 0;
}
if (ns_attacks[chn][2] && ns_attacks[chn][1]) {
ns_attacks[chn][2] = 0;
}
if (ns_attacks[chn][3] && ns_attacks[chn][2]) {
ns_attacks[chn][3] = 0;
}
}
if (chn < 2) {
uselongblock[chn] = ns_uselongblock;
}
else {
if (ns_uselongblock == 0) {
uselongblock[0] = uselongblock[1] = 0;
}
}
/* there is a one granule delay. Copy maskings computed last call
* into masking_ratio to return to calling program.
*/
energy[chn] = psv->tot_ener[chn];
}
}
static void
vbrpsy_skip_masking_s(lame_internal_flags * gfc, int chn, int sblock)
{
if (sblock == 0) {
FLOAT *nbs2 = &gfc->sv_psy.nb_s2[chn][0];
FLOAT *nbs1 = &gfc->sv_psy.nb_s1[chn][0];
int const n = gfc->cd_psy->s.npart;
int b;
for (b = 0; b < n; b++) {
nbs2[b] = nbs1[b];
}
}
}
static void
vbrpsy_calc_mask_index_s(lame_internal_flags const *gfc, FLOAT const *max,
FLOAT const *avg, unsigned char *mask_idx)
{
PsyConst_CB2SB_t const *const gds = &gfc->cd_psy->s;
FLOAT m, a;
int b, k;
int const last_tab_entry = dimension_of(tab) - 1;
b = 0;
a = avg[b] + avg[b + 1];
assert(a >= 0);
if (a > 0.0f) {
m = max[b];
if (m < max[b + 1])
m = max[b + 1];
assert((gds->numlines[b] + gds->numlines[b + 1] - 1) > 0);
a = 20.0f * (m * 2.0f - a)
/ (a * (gds->numlines[b] + gds->numlines[b + 1] - 1));
k = (int) a;
if (k > last_tab_entry)
k = last_tab_entry;
mask_idx[b] = k;
}
else {
mask_idx[b] = 0;
}
for (b = 1; b < gds->npart - 1; b++) {
a = avg[b - 1] + avg[b] + avg[b + 1];
assert(b + 1 < gds->npart);
assert(a >= 0);
if (a > 0.0) {
m = max[b - 1];