-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquantize.c
More file actions
2050 lines (1732 loc) · 64.2 KB
/
quantize.c
File metadata and controls
2050 lines (1732 loc) · 64.2 KB
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
/*
* MP3 quantization
*
* Copyright (c) 1999-2000 Mark Taylor
* Copyright (c) 1999-2003 Takehiro Tominaga
* Copyright (c) 2000-2011 Robert Hegemann
* Copyright (c) 2001-2005 Gabriel Bouvigne
*
* 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: quantize.c,v 1.216.2.1 2012/01/08 23:49:58 robert Exp $ */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "lame.h"
#include "machine.h"
#include "encoder.h"
#include "util.h"
#include "quantize_pvt.h"
#include "reservoir.h"
#include "bitstream.h"
#include "vbrquantize.h"
#include "quantize.h"
#ifdef HAVE_XMMINTRIN_H
#include "vector/lame_intrin.h"
#endif
/* convert from L/R <-> Mid/Side */
static void
ms_convert(III_side_info_t * l3_side, int gr)
{
int i;
for (i = 0; i < 576; ++i) {
FLOAT l, r;
l = l3_side->tt[gr][0].xr[i];
r = l3_side->tt[gr][1].xr[i];
l3_side->tt[gr][0].xr[i] = (l + r) * (FLOAT) (SQRT2 * 0.5);
l3_side->tt[gr][1].xr[i] = (l - r) * (FLOAT) (SQRT2 * 0.5);
}
}
/************************************************************************
*
* init_outer_loop()
* mt 6/99
*
* initializes cod_info, scalefac and xrpow
*
* returns 0 if all energies in xr are zero, else 1
*
************************************************************************/
static void
init_xrpow_core_c(gr_info * const cod_info, FLOAT xrpow[576], int upper, FLOAT * sum)
{
int i;
FLOAT tmp;
*sum = 0;
for (i = 0; i <= upper; ++i) {
tmp = fabs(cod_info->xr[i]);
*sum += tmp;
xrpow[i] = sqrt(tmp * sqrt(tmp));
if (xrpow[i] > cod_info->xrpow_max)
cod_info->xrpow_max = xrpow[i];
}
}
void
init_xrpow_core_init(lame_internal_flags * const gfc)
{
gfc->init_xrpow_core = init_xrpow_core_c;
#if defined(HAVE_XMMINTRIN_H)
if (gfc->CPU_features.SSE)
gfc->init_xrpow_core = init_xrpow_core_sse;
#endif
#ifndef HAVE_NASM
#ifdef MIN_ARCH_SSE
gfc->init_xrpow_core = init_xrpow_core_sse;
#endif
#endif
}
static int
init_xrpow(lame_internal_flags * gfc, gr_info * const cod_info, FLOAT xrpow[576])
{
FLOAT sum = 0;
int i;
int const upper = cod_info->max_nonzero_coeff;
assert(xrpow != NULL);
cod_info->xrpow_max = 0;
/* check if there is some energy we have to quantize
* and calculate xrpow matching our fresh scalefactors
*/
assert(0 <= upper && upper <= 575);
memset(&(xrpow[upper]), 0, (576 - upper) * sizeof(xrpow[0]));
gfc->init_xrpow_core(cod_info, xrpow, upper, &sum);
/* return 1 if we have something to quantize, else 0
*/
if (sum > (FLOAT) 1E-20) {
int j = 0;
if (gfc->sv_qnt.substep_shaping & 2)
j = 1;
for (i = 0; i < cod_info->psymax; i++)
gfc->sv_qnt.pseudohalf[i] = j;
return 1;
}
memset(&cod_info->l3_enc[0], 0, sizeof(int) * 576);
return 0;
}
/*
Gabriel Bouvigne feb/apr 2003
Analog silence detection in partitionned sfb21
or sfb12 for short blocks
From top to bottom of sfb, changes to 0
coeffs which are below ath. It stops on the first
coeff higher than ath.
*/
static void
psfb21_analogsilence(lame_internal_flags const *gfc, gr_info * const cod_info)
{
ATH_t const *const ATH = gfc->ATH;
FLOAT *const xr = cod_info->xr;
if (cod_info->block_type != SHORT_TYPE) { /* NORM, START or STOP type, but not SHORT blocks */
int gsfb;
int stop = 0;
for (gsfb = PSFB21 - 1; gsfb >= 0 && !stop; gsfb--) {
int const start = gfc->scalefac_band.psfb21[gsfb];
int const end = gfc->scalefac_band.psfb21[gsfb + 1];
int j;
FLOAT ath21;
ath21 = athAdjust(ATH->adjust_factor, ATH->psfb21[gsfb], ATH->floor, 0);
if (gfc->sv_qnt.longfact[21] > 1e-12f)
ath21 *= gfc->sv_qnt.longfact[21];
for (j = end - 1; j >= start; j--) {
if (fabs(xr[j]) < ath21)
xr[j] = 0;
else {
stop = 1;
break;
}
}
}
}
else {
/*note: short blocks coeffs are reordered */
int block;
for (block = 0; block < 3; block++) {
int gsfb;
int stop = 0;
for (gsfb = PSFB12 - 1; gsfb >= 0 && !stop; gsfb--) {
int const start = gfc->scalefac_band.s[12] * 3 +
(gfc->scalefac_band.s[13] - gfc->scalefac_band.s[12]) * block +
(gfc->scalefac_band.psfb12[gsfb] - gfc->scalefac_band.psfb12[0]);
int const end =
start + (gfc->scalefac_band.psfb12[gsfb + 1] - gfc->scalefac_band.psfb12[gsfb]);
int j;
FLOAT ath12;
ath12 = athAdjust(ATH->adjust_factor, ATH->psfb12[gsfb], ATH->floor, 0);
if (gfc->sv_qnt.shortfact[12] > 1e-12f)
ath12 *= gfc->sv_qnt.shortfact[12];
for (j = end - 1; j >= start; j--) {
if (fabs(xr[j]) < ath12)
xr[j] = 0;
else {
stop = 1;
break;
}
}
}
}
}
}
static void
init_outer_loop(lame_internal_flags const *gfc, gr_info * const cod_info)
{
SessionConfig_t const *const cfg = &gfc->cfg;
int sfb, j;
/* initialize fresh cod_info
*/
cod_info->part2_3_length = 0;
cod_info->big_values = 0;
cod_info->count1 = 0;
cod_info->global_gain = 210;
cod_info->scalefac_compress = 0;
/* mixed_block_flag, block_type was set in psymodel.c */
cod_info->table_select[0] = 0;
cod_info->table_select[1] = 0;
cod_info->table_select[2] = 0;
cod_info->subblock_gain[0] = 0;
cod_info->subblock_gain[1] = 0;
cod_info->subblock_gain[2] = 0;
cod_info->subblock_gain[3] = 0; /* this one is always 0 */
cod_info->region0_count = 0;
cod_info->region1_count = 0;
cod_info->preflag = 0;
cod_info->scalefac_scale = 0;
cod_info->count1table_select = 0;
cod_info->part2_length = 0;
if (cfg->samplerate_out <= 8000) {
cod_info->sfb_lmax = 17;
cod_info->sfb_smin = 9;
cod_info->psy_lmax = 17;
}
else {
cod_info->sfb_lmax = SBPSY_l;
cod_info->sfb_smin = SBPSY_s;
cod_info->psy_lmax = gfc->sv_qnt.sfb21_extra ? SBMAX_l : SBPSY_l;
}
cod_info->psymax = cod_info->psy_lmax;
cod_info->sfbmax = cod_info->sfb_lmax;
cod_info->sfbdivide = 11;
for (sfb = 0; sfb < SBMAX_l; sfb++) {
cod_info->width[sfb]
= gfc->scalefac_band.l[sfb + 1] - gfc->scalefac_band.l[sfb];
cod_info->window[sfb] = 3; /* which is always 0. */
}
if (cod_info->block_type == SHORT_TYPE) {
FLOAT ixwork[576];
FLOAT *ix;
cod_info->sfb_smin = 0;
cod_info->sfb_lmax = 0;
if (cod_info->mixed_block_flag) {
/*
* MPEG-1: sfbs 0-7 long block, 3-12 short blocks
* MPEG-2(.5): sfbs 0-5 long block, 3-12 short blocks
*/
cod_info->sfb_smin = 3;
cod_info->sfb_lmax = cfg->mode_gr * 2 + 4;
}
if (cfg->samplerate_out <= 8000) {
cod_info->psymax
= cod_info->sfb_lmax
+ 3 * (9 - cod_info->sfb_smin);
cod_info->sfbmax = cod_info->sfb_lmax + 3 * (9 - cod_info->sfb_smin);
}
else {
cod_info->psymax
= cod_info->sfb_lmax
+ 3 * ((gfc->sv_qnt.sfb21_extra ? SBMAX_s : SBPSY_s) - cod_info->sfb_smin);
cod_info->sfbmax = cod_info->sfb_lmax + 3 * (SBPSY_s - cod_info->sfb_smin);
}
cod_info->sfbdivide = cod_info->sfbmax - 18;
cod_info->psy_lmax = cod_info->sfb_lmax;
/* re-order the short blocks, for more efficient encoding below */
/* By Takehiro TOMINAGA */
/*
Within each scalefactor band, data is given for successive
time windows, beginning with window 0 and ending with window 2.
Within each window, the quantized values are then arranged in
order of increasing frequency...
*/
ix = &cod_info->xr[gfc->scalefac_band.l[cod_info->sfb_lmax]];
memcpy(ixwork, cod_info->xr, 576 * sizeof(FLOAT));
for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
int const start = gfc->scalefac_band.s[sfb];
int const end = gfc->scalefac_band.s[sfb + 1];
int window, l;
for (window = 0; window < 3; window++) {
for (l = start; l < end; l++) {
*ix++ = ixwork[3 * l + window];
}
}
}
j = cod_info->sfb_lmax;
for (sfb = cod_info->sfb_smin; sfb < SBMAX_s; sfb++) {
cod_info->width[j] = cod_info->width[j + 1] = cod_info->width[j + 2]
= gfc->scalefac_band.s[sfb + 1] - gfc->scalefac_band.s[sfb];
cod_info->window[j] = 0;
cod_info->window[j + 1] = 1;
cod_info->window[j + 2] = 2;
j += 3;
}
}
cod_info->count1bits = 0;
cod_info->sfb_partition_table = nr_of_sfb_block[0][0];
cod_info->slen[0] = 0;
cod_info->slen[1] = 0;
cod_info->slen[2] = 0;
cod_info->slen[3] = 0;
cod_info->max_nonzero_coeff = 575;
/* fresh scalefactors are all zero
*/
memset(cod_info->scalefac, 0, sizeof(cod_info->scalefac));
if (cfg->vbr != vbr_mt && cfg->vbr != vbr_mtrh && cfg->vbr != vbr_abr && cfg->vbr != vbr_off) {
psfb21_analogsilence(gfc, cod_info);
}
}
/************************************************************************
*
* bin_search_StepSize()
*
* author/date??
*
* binary step size search
* used by outer_loop to get a quantizer step size to start with
*
************************************************************************/
typedef enum {
BINSEARCH_NONE,
BINSEARCH_UP,
BINSEARCH_DOWN
} binsearchDirection_t;
static int
bin_search_StepSize(lame_internal_flags * const gfc, gr_info * const cod_info,
int desired_rate, const int ch, const FLOAT xrpow[576])
{
int nBits;
int CurrentStep = gfc->sv_qnt.CurrentStep[ch];
int flag_GoneOver = 0;
int const start = gfc->sv_qnt.OldValue[ch];
binsearchDirection_t Direction = BINSEARCH_NONE;
cod_info->global_gain = start;
desired_rate -= cod_info->part2_length;
assert(CurrentStep);
for (;;) {
int step;
nBits = count_bits(gfc, xrpow, cod_info, 0);
if (CurrentStep == 1 || nBits == desired_rate)
break; /* nothing to adjust anymore */
if (nBits > desired_rate) {
/* increase Quantize_StepSize */
if (Direction == BINSEARCH_DOWN)
flag_GoneOver = 1;
if (flag_GoneOver)
CurrentStep /= 2;
Direction = BINSEARCH_UP;
step = CurrentStep;
}
else {
/* decrease Quantize_StepSize */
if (Direction == BINSEARCH_UP)
flag_GoneOver = 1;
if (flag_GoneOver)
CurrentStep /= 2;
Direction = BINSEARCH_DOWN;
step = -CurrentStep;
}
cod_info->global_gain += step;
if (cod_info->global_gain < 0) {
cod_info->global_gain = 0;
flag_GoneOver = 1;
}
if (cod_info->global_gain > 255) {
cod_info->global_gain = 255;
flag_GoneOver = 1;
}
}
assert(cod_info->global_gain >= 0);
assert(cod_info->global_gain < 256);
while (nBits > desired_rate && cod_info->global_gain < 255) {
cod_info->global_gain++;
nBits = count_bits(gfc, xrpow, cod_info, 0);
}
gfc->sv_qnt.CurrentStep[ch] = (start - cod_info->global_gain >= 4) ? 4 : 2;
gfc->sv_qnt.OldValue[ch] = cod_info->global_gain;
cod_info->part2_3_length = nBits;
return nBits;
}
/************************************************************************
*
* trancate_smallspectrums()
*
* Takehiro TOMINAGA 2002-07-21
*
* trancate smaller nubmers into 0 as long as the noise threshold is allowed.
*
************************************************************************/
static int
floatcompare(const void *v1, const void *v2)
{
const FLOAT *const a = v1, *const b = v2;
if (*a > *b)
return 1;
if (*a < *b)
return -1;
return 0;
}
static void
trancate_smallspectrums(lame_internal_flags const *gfc,
gr_info * const gi, const FLOAT * const l3_xmin, FLOAT * const work)
{
int sfb, j, width;
FLOAT distort[SFBMAX];
calc_noise_result dummy;
if ((!(gfc->sv_qnt.substep_shaping & 4) && gi->block_type == SHORT_TYPE)
|| gfc->sv_qnt.substep_shaping & 0x80)
return;
(void) calc_noise(gi, l3_xmin, distort, &dummy, 0);
for (j = 0; j < 576; j++) {
FLOAT xr = 0.0;
if (gi->l3_enc[j] != 0)
xr = fabs(gi->xr[j]);
work[j] = xr;
}
j = 0;
sfb = 8;
if (gi->block_type == SHORT_TYPE)
sfb = 6;
do {
FLOAT allowedNoise, trancateThreshold;
int nsame, start;
width = gi->width[sfb];
j += width;
if (distort[sfb] >= 1.0)
continue;
qsort(&work[j - width], width, sizeof(FLOAT), floatcompare);
if (EQ(work[j - 1], 0.0))
continue; /* all zero sfb */
allowedNoise = (1.0 - distort[sfb]) * l3_xmin[sfb];
trancateThreshold = 0.0;
start = 0;
do {
FLOAT noise;
for (nsame = 1; start + nsame < width; nsame++)
if (NEQ(work[start + j - width], work[start + j + nsame - width]))
break;
noise = work[start + j - width] * work[start + j - width] * nsame;
if (allowedNoise < noise) {
if (start != 0)
trancateThreshold = work[start + j - width - 1];
break;
}
allowedNoise -= noise;
start += nsame;
} while (start < width);
if (EQ(trancateThreshold, 0.0))
continue;
/* printf("%e %e %e\n", */
/* trancateThreshold/l3_xmin[sfb], */
/* trancateThreshold/(l3_xmin[sfb]*start), */
/* trancateThreshold/(l3_xmin[sfb]*(start+width)) */
/* ); */
/* if (trancateThreshold > 1000*l3_xmin[sfb]*start) */
/* trancateThreshold = 1000*l3_xmin[sfb]*start; */
do {
if (fabs(gi->xr[j - width]) <= trancateThreshold)
gi->l3_enc[j - width] = 0;
} while (--width > 0);
} while (++sfb < gi->psymax);
gi->part2_3_length = noquant_count_bits(gfc, gi, 0);
}
/*************************************************************************
*
* loop_break()
*
* author/date??
*
* Function: Returns zero if there is a scalefac which has not been
* amplified. Otherwise it returns one.
*
*************************************************************************/
inline static int
loop_break(const gr_info * const cod_info)
{
int sfb;
for (sfb = 0; sfb < cod_info->sfbmax; sfb++)
if (cod_info->scalefac[sfb]
+ cod_info->subblock_gain[cod_info->window[sfb]] == 0)
return 0;
return 1;
}
/* mt 5/99: Function: Improved calc_noise for a single channel */
/*************************************************************************
*
* quant_compare()
*
* author/date??
*
* several different codes to decide which quantization is better
*
*************************************************************************/
static double
penalties(double noise)
{
return FAST_LOG10(0.368 + 0.632 * noise * noise * noise);
}
static double
get_klemm_noise(const FLOAT * distort, const gr_info * const gi)
{
int sfb;
double klemm_noise = 1E-37;
for (sfb = 0; sfb < gi->psymax; sfb++)
klemm_noise += penalties(distort[sfb]);
return Max(1e-20, klemm_noise);
}
inline static int
quant_compare(const int quant_comp,
const calc_noise_result * const best,
calc_noise_result * const calc, const gr_info * const gi, const FLOAT * distort)
{
/*
noise is given in decibels (dB) relative to masking thesholds.
over_noise: ??? (the previous comment is fully wrong)
tot_noise: ??? (the previous comment is fully wrong)
max_noise: max quantization noise
*/
int better;
switch (quant_comp) {
default:
case 9:{
if (best->over_count > 0) {
/* there are distorted sfb */
better = calc->over_SSD <= best->over_SSD;
if (calc->over_SSD == best->over_SSD)
better = calc->bits < best->bits;
}
else {
/* no distorted sfb */
better = ((calc->max_noise < 0) &&
((calc->max_noise * 10 + calc->bits) <=
(best->max_noise * 10 + best->bits)));
}
break;
}
case 0:
better = calc->over_count < best->over_count
|| (calc->over_count == best->over_count && calc->over_noise < best->over_noise)
|| (calc->over_count == best->over_count &&
EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
break;
case 8:
calc->max_noise = get_klemm_noise(distort, gi);
/*lint --fallthrough */
case 1:
better = calc->max_noise < best->max_noise;
break;
case 2:
better = calc->tot_noise < best->tot_noise;
break;
case 3:
better = (calc->tot_noise < best->tot_noise)
&& (calc->max_noise < best->max_noise);
break;
case 4:
better = (calc->max_noise <= 0.0 && best->max_noise > 0.2)
|| (calc->max_noise <= 0.0 &&
best->max_noise < 0.0 &&
best->max_noise > calc->max_noise - 0.2 && calc->tot_noise < best->tot_noise)
|| (calc->max_noise <= 0.0 &&
best->max_noise > 0.0 &&
best->max_noise > calc->max_noise - 0.2 &&
calc->tot_noise < best->tot_noise + best->over_noise)
|| (calc->max_noise > 0.0 &&
best->max_noise > -0.05 &&
best->max_noise > calc->max_noise - 0.1 &&
calc->tot_noise + calc->over_noise < best->tot_noise + best->over_noise)
|| (calc->max_noise > 0.0 &&
best->max_noise > -0.1 &&
best->max_noise > calc->max_noise - 0.15 &&
calc->tot_noise + calc->over_noise + calc->over_noise <
best->tot_noise + best->over_noise + best->over_noise);
break;
case 5:
better = calc->over_noise < best->over_noise
|| (EQ(calc->over_noise, best->over_noise) && calc->tot_noise < best->tot_noise);
break;
case 6:
better = calc->over_noise < best->over_noise
|| (EQ(calc->over_noise, best->over_noise) &&
(calc->max_noise < best->max_noise
|| (EQ(calc->max_noise, best->max_noise) && calc->tot_noise <= best->tot_noise)
));
break;
case 7:
better = calc->over_count < best->over_count || calc->over_noise < best->over_noise;
break;
}
if (best->over_count == 0) {
/*
If no distorted bands, only use this quantization
if it is better, and if it uses less bits.
Unfortunately, part2_3_length is sometimes a poor
estimator of the final size at low bitrates.
*/
better = better && calc->bits < best->bits;
}
return better;
}
/*************************************************************************
*
* amp_scalefac_bands()
*
* author/date??
*
* Amplify the scalefactor bands that violate the masking threshold.
* See ISO 11172-3 Section C.1.5.4.3.5
*
* distort[] = noise/masking
* distort[] > 1 ==> noise is not masked
* distort[] < 1 ==> noise is masked
* max_dist = maximum value of distort[]
*
* Three algorithms:
* noise_shaping_amp
* 0 Amplify all bands with distort[]>1.
*
* 1 Amplify all bands with distort[] >= max_dist^(.5);
* ( 50% in the db scale)
*
* 2 Amplify first band with distort[] >= max_dist;
*
*
* For algorithms 0 and 1, if max_dist < 1, then amplify all bands
* with distort[] >= .95*max_dist. This is to make sure we always
* amplify at least one band.
*
*
*************************************************************************/
static void
amp_scalefac_bands(lame_internal_flags * gfc,
gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
{
SessionConfig_t const *const cfg = &gfc->cfg;
int j, sfb;
FLOAT ifqstep34, trigger;
int noise_shaping_amp;
if (cod_info->scalefac_scale == 0) {
ifqstep34 = 1.29683955465100964055; /* 2**(.75*.5) */
}
else {
ifqstep34 = 1.68179283050742922612; /* 2**(.75*1) */
}
/* compute maximum value of distort[] */
trigger = 0;
for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
if (trigger < distort[sfb])
trigger = distort[sfb];
}
noise_shaping_amp = cfg->noise_shaping_amp;
if (noise_shaping_amp == 3) {
if (bRefine == 1)
noise_shaping_amp = 2;
else
noise_shaping_amp = 1;
}
switch (noise_shaping_amp) {
case 2:
/* amplify exactly 1 band */
break;
case 1:
/* amplify bands within 50% of max (on db scale) */
if (trigger > 1.0)
trigger = pow(trigger, .5);
else
trigger *= .95;
break;
case 0:
default:
/* ISO algorithm. amplify all bands with distort>1 */
if (trigger > 1.0)
trigger = 1.0;
else
trigger *= .95;
break;
}
j = 0;
for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
int const width = cod_info->width[sfb];
int l;
j += width;
if (distort[sfb] < trigger)
continue;
if (gfc->sv_qnt.substep_shaping & 2) {
gfc->sv_qnt.pseudohalf[sfb] = !gfc->sv_qnt.pseudohalf[sfb];
if (!gfc->sv_qnt.pseudohalf[sfb] && cfg->noise_shaping_amp == 2)
return;
}
cod_info->scalefac[sfb]++;
for (l = -width; l < 0; l++) {
xrpow[j + l] *= ifqstep34;
if (xrpow[j + l] > cod_info->xrpow_max)
cod_info->xrpow_max = xrpow[j + l];
}
if (cfg->noise_shaping_amp == 2)
return;
}
}
/*************************************************************************
*
* inc_scalefac_scale()
*
* Takehiro Tominaga 2000-xx-xx
*
* turns on scalefac scale and adjusts scalefactors
*
*************************************************************************/
static void
inc_scalefac_scale(gr_info * const cod_info, FLOAT xrpow[576])
{
int l, j, sfb;
const FLOAT ifqstep34 = 1.29683955465100964055;
j = 0;
for (sfb = 0; sfb < cod_info->sfbmax; sfb++) {
int const width = cod_info->width[sfb];
int s = cod_info->scalefac[sfb];
if (cod_info->preflag)
s += pretab[sfb];
j += width;
if (s & 1) {
s++;
for (l = -width; l < 0; l++) {
xrpow[j + l] *= ifqstep34;
if (xrpow[j + l] > cod_info->xrpow_max)
cod_info->xrpow_max = xrpow[j + l];
}
}
cod_info->scalefac[sfb] = s >> 1;
}
cod_info->preflag = 0;
cod_info->scalefac_scale = 1;
}
/*************************************************************************
*
* inc_subblock_gain()
*
* Takehiro Tominaga 2000-xx-xx
*
* increases the subblock gain and adjusts scalefactors
*
*************************************************************************/
static int
inc_subblock_gain(const lame_internal_flags * const gfc, gr_info * const cod_info, FLOAT xrpow[576])
{
int sfb, window;
int *const scalefac = cod_info->scalefac;
/* subbloc_gain can't do anything in the long block region */
for (sfb = 0; sfb < cod_info->sfb_lmax; sfb++) {
if (scalefac[sfb] >= 16)
return 1;
}
for (window = 0; window < 3; window++) {
int s1, s2, l, j;
s1 = s2 = 0;
for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbdivide; sfb += 3) {
if (s1 < scalefac[sfb])
s1 = scalefac[sfb];
}
for (; sfb < cod_info->sfbmax; sfb += 3) {
if (s2 < scalefac[sfb])
s2 = scalefac[sfb];
}
if (s1 < 16 && s2 < 8)
continue;
if (cod_info->subblock_gain[window] >= 7)
return 1;
/* even though there is no scalefactor for sfb12
* subblock gain affects upper frequencies too, that's why
* we have to go up to SBMAX_s
*/
cod_info->subblock_gain[window]++;
j = gfc->scalefac_band.l[cod_info->sfb_lmax];
for (sfb = cod_info->sfb_lmax + window; sfb < cod_info->sfbmax; sfb += 3) {
FLOAT amp;
int const width = cod_info->width[sfb];
int s = scalefac[sfb];
assert(s >= 0);
s = s - (4 >> cod_info->scalefac_scale);
if (s >= 0) {
scalefac[sfb] = s;
j += width * 3;
continue;
}
scalefac[sfb] = 0;
{
int const gain = 210 + (s << (cod_info->scalefac_scale + 1));
amp = IPOW20(gain);
}
j += width * (window + 1);
for (l = -width; l < 0; l++) {
xrpow[j + l] *= amp;
if (xrpow[j + l] > cod_info->xrpow_max)
cod_info->xrpow_max = xrpow[j + l];
}
j += width * (3 - window - 1);
}
{
FLOAT const amp = IPOW20(202);
j += cod_info->width[sfb] * (window + 1);
for (l = -cod_info->width[sfb]; l < 0; l++) {
xrpow[j + l] *= amp;
if (xrpow[j + l] > cod_info->xrpow_max)
cod_info->xrpow_max = xrpow[j + l];
}
}
}
return 0;
}
/********************************************************************
*
* balance_noise()
*
* Takehiro Tominaga /date??
* Robert Hegemann 2000-09-06: made a function of it
*
* amplifies scalefactor bands,
* - if all are already amplified returns 0
* - if some bands are amplified too much:
* * try to increase scalefac_scale
* * if already scalefac_scale was set
* try on short blocks to increase subblock gain
*
********************************************************************/
inline static int
balance_noise(lame_internal_flags * gfc,
gr_info * const cod_info, FLOAT const *distort, FLOAT xrpow[576], int bRefine)
{
SessionConfig_t const *const cfg = &gfc->cfg;
int status;
amp_scalefac_bands(gfc, cod_info, distort, xrpow, bRefine);
/* check to make sure we have not amplified too much
* loop_break returns 0 if there is an unamplified scalefac
* scale_bitcount returns 0 if no scalefactors are too large
*/
status = loop_break(cod_info);
if (status)
return 0; /* all bands amplified */
/* not all scalefactors have been amplified. so these
* scalefacs are possibly valid. encode them:
*/
status = scale_bitcount(gfc, cod_info);
if (!status)
return 1; /* amplified some bands not exceeding limits */
/* some scalefactors are too large.
* lets try setting scalefac_scale=1
*/
if (cfg->noise_shaping > 1) {
memset(&gfc->sv_qnt.pseudohalf[0], 0, sizeof(gfc->sv_qnt.pseudohalf));
if (!cod_info->scalefac_scale) {
inc_scalefac_scale(cod_info, xrpow);
status = 0;
}
else {
if (cod_info->block_type == SHORT_TYPE && cfg->subblock_gain > 0) {
status = inc_subblock_gain(gfc, cod_info, xrpow)
|| loop_break(cod_info);
}
}
}
if (!status) {
status = scale_bitcount(gfc, cod_info);
}
return !status;
}
/************************************************************************
*
* outer_loop ()
*
* Function: The outer iteration loop controls the masking conditions
* of all scalefactorbands. It computes the best scalefac and
* global gain. This module calls the inner iteration loop
*
* mt 5/99 completely rewritten to allow for bit reservoir control,