-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtakehiro.c
More file actions
1375 lines (1139 loc) · 37.3 KB
/
takehiro.c
File metadata and controls
1375 lines (1139 loc) · 37.3 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 huffman table selecting and bit counting
*
* Copyright (c) 1999-2005 Takehiro TOMINAGA
* Copyright (c) 2002-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: takehiro.c,v 1.79 2011/05/07 16:05:17 rbrito 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 "tables.h"
static const struct {
const int region0_count;
const int region1_count;
} subdv_table[23] = {
{
0, 0}, /* 0 bands */
{
0, 0}, /* 1 bands */
{
0, 0}, /* 2 bands */
{
0, 0}, /* 3 bands */
{
0, 0}, /* 4 bands */
{
0, 1}, /* 5 bands */
{
1, 1}, /* 6 bands */
{
1, 1}, /* 7 bands */
{
1, 2}, /* 8 bands */
{
2, 2}, /* 9 bands */
{
2, 3}, /* 10 bands */
{
2, 3}, /* 11 bands */
{
3, 4}, /* 12 bands */
{
3, 4}, /* 13 bands */
{
3, 4}, /* 14 bands */
{
4, 5}, /* 15 bands */
{
4, 5}, /* 16 bands */
{
4, 6}, /* 17 bands */
{
5, 6}, /* 18 bands */
{
5, 6}, /* 19 bands */
{
5, 7}, /* 20 bands */
{
6, 7}, /* 21 bands */
{
6, 7}, /* 22 bands */
};
/*********************************************************************
* nonlinear quantization of xr
* More accurate formula than the ISO formula. Takes into account
* the fact that we are quantizing xr -> ix, but we want ix^4/3 to be
* as close as possible to x^4/3. (taking the nearest int would mean
* ix is as close as possible to xr, which is different.)
*
* From Segher Boessenkool <segher@eastsite.nl> 11/1999
*
* 09/2000: ASM code removed in favor of IEEE754 hack by Takehiro
* Tominaga. If you need the ASM code, check CVS circa Aug 2000.
*
* 01/2004: Optimizations by Gabriel Bouvigne
*********************************************************************/
static void
quantize_lines_xrpow_01(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
{
const FLOAT compareval0 = (1.0f - 0.4054f) / istep;
unsigned int i;
assert(l > 0);
assert(l % 2 == 0);
for (i = 0; i < l; i += 2) {
FLOAT const xr_0 = xr[i+0];
FLOAT const xr_1 = xr[i+1];
int const ix_0 = (compareval0 > xr_0) ? 0 : 1;
int const ix_1 = (compareval0 > xr_1) ? 0 : 1;
ix[i+0] = ix_0;
ix[i+1] = ix_1;
}
}
#ifdef TAKEHIRO_IEEE754_HACK
typedef union {
float f;
int i;
} fi_union;
#define MAGIC_FLOAT (65536*(128))
#define MAGIC_INT 0x4b000000
static void
quantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xp, int *pi)
{
fi_union *fi;
unsigned int remaining;
assert(l > 0);
fi = (fi_union *) pi;
l = l >> 1;
remaining = l % 2;
l = l >> 1;
while (l--) {
double x0 = istep * xp[0];
double x1 = istep * xp[1];
double x2 = istep * xp[2];
double x3 = istep * xp[3];
x0 += MAGIC_FLOAT;
fi[0].f = x0;
x1 += MAGIC_FLOAT;
fi[1].f = x1;
x2 += MAGIC_FLOAT;
fi[2].f = x2;
x3 += MAGIC_FLOAT;
fi[3].f = x3;
fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
fi[2].f = x2 + adj43asm[fi[2].i - MAGIC_INT];
fi[3].f = x3 + adj43asm[fi[3].i - MAGIC_INT];
fi[0].i -= MAGIC_INT;
fi[1].i -= MAGIC_INT;
fi[2].i -= MAGIC_INT;
fi[3].i -= MAGIC_INT;
fi += 4;
xp += 4;
};
if (remaining) {
double x0 = istep * xp[0];
double x1 = istep * xp[1];
x0 += MAGIC_FLOAT;
fi[0].f = x0;
x1 += MAGIC_FLOAT;
fi[1].f = x1;
fi[0].f = x0 + adj43asm[fi[0].i - MAGIC_INT];
fi[1].f = x1 + adj43asm[fi[1].i - MAGIC_INT];
fi[0].i -= MAGIC_INT;
fi[1].i -= MAGIC_INT;
}
}
#else
/*********************************************************************
* XRPOW_FTOI is a macro to convert floats to ints.
* if XRPOW_FTOI(x) = nearest_int(x), then QUANTFAC(x)=adj43asm[x]
* ROUNDFAC= -0.0946
*
* if XRPOW_FTOI(x) = floor(x), then QUANTFAC(x)=asj43[x]
* ROUNDFAC=0.4054
*
* Note: using floor() or (int) is extremely slow. On machines where
* the TAKEHIRO_IEEE754_HACK code above does not work, it is worthwile
* to write some ASM for XRPOW_FTOI().
*********************************************************************/
#define XRPOW_FTOI(src,dest) ((dest) = (int)(src))
#define QUANTFAC(rx) adj43[rx]
#define ROUNDFAC 0.4054
static void
quantize_lines_xrpow(unsigned int l, FLOAT istep, const FLOAT * xr, int *ix)
{
unsigned int remaining;
assert(l > 0);
l = l >> 1;
remaining = l % 2;
l = l >> 1;
while (l--) {
FLOAT x0, x1, x2, x3;
int rx0, rx1, rx2, rx3;
x0 = *xr++ * istep;
x1 = *xr++ * istep;
XRPOW_FTOI(x0, rx0);
x2 = *xr++ * istep;
XRPOW_FTOI(x1, rx1);
x3 = *xr++ * istep;
XRPOW_FTOI(x2, rx2);
x0 += QUANTFAC(rx0);
XRPOW_FTOI(x3, rx3);
x1 += QUANTFAC(rx1);
XRPOW_FTOI(x0, *ix++);
x2 += QUANTFAC(rx2);
XRPOW_FTOI(x1, *ix++);
x3 += QUANTFAC(rx3);
XRPOW_FTOI(x2, *ix++);
XRPOW_FTOI(x3, *ix++);
};
if (remaining) {
FLOAT x0, x1;
int rx0, rx1;
x0 = *xr++ * istep;
x1 = *xr++ * istep;
XRPOW_FTOI(x0, rx0);
XRPOW_FTOI(x1, rx1);
x0 += QUANTFAC(rx0);
x1 += QUANTFAC(rx1);
XRPOW_FTOI(x0, *ix++);
XRPOW_FTOI(x1, *ix++);
}
}
#endif
/*********************************************************************
* Quantization function
* This function will select which lines to quantize and call the
* proper quantization function
*********************************************************************/
static void
quantize_xrpow(const FLOAT * xp, int *pi, FLOAT istep, gr_info const *const cod_info,
calc_noise_data const *prev_noise)
{
/* quantize on xr^(3/4) instead of xr */
int sfb;
int sfbmax;
int j = 0;
int prev_data_use;
int *iData;
int accumulate = 0;
int accumulate01 = 0;
int *acc_iData;
const FLOAT *acc_xp;
iData = pi;
acc_xp = xp;
acc_iData = iData;
/* Reusing previously computed data does not seems to work if global gain
is changed. Finding why it behaves this way would allow to use a cache of
previously computed values (let's 10 cached values per sfb) that would
probably provide a noticeable speedup */
prev_data_use = (prev_noise && (cod_info->global_gain == prev_noise->global_gain));
if (cod_info->block_type == SHORT_TYPE)
sfbmax = 38;
else
sfbmax = 21;
for (sfb = 0; sfb <= sfbmax; sfb++) {
int step = -1;
if (prev_data_use || cod_info->block_type == NORM_TYPE) {
step =
cod_info->global_gain
- ((cod_info->scalefac[sfb] + (cod_info->preflag ? pretab[sfb] : 0))
<< (cod_info->scalefac_scale + 1))
- cod_info->subblock_gain[cod_info->window[sfb]] * 8;
}
assert(cod_info->width[sfb] >= 0);
if (prev_data_use && (prev_noise->step[sfb] == step)) {
/* do not recompute this part,
but compute accumulated lines */
if (accumulate) {
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
accumulate = 0;
}
if (accumulate01) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
accumulate01 = 0;
}
}
else { /*should compute this part */
int l;
l = cod_info->width[sfb];
if ((j + cod_info->width[sfb]) > cod_info->max_nonzero_coeff) {
/*do not compute upper zero part */
int usefullsize;
usefullsize = cod_info->max_nonzero_coeff - j + 1;
memset(&pi[cod_info->max_nonzero_coeff], 0,
sizeof(int) * (576 - cod_info->max_nonzero_coeff));
l = usefullsize;
if (l < 0) {
l = 0;
}
/* no need to compute higher sfb values */
sfb = sfbmax + 1;
}
/*accumulate lines to quantize */
if (!accumulate && !accumulate01) {
acc_iData = iData;
acc_xp = xp;
}
if (prev_noise &&
prev_noise->sfb_count1 > 0 &&
sfb >= prev_noise->sfb_count1 &&
prev_noise->step[sfb] > 0 && step >= prev_noise->step[sfb]) {
if (accumulate) {
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
accumulate = 0;
acc_iData = iData;
acc_xp = xp;
}
accumulate01 += l;
}
else {
if (accumulate01) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
accumulate01 = 0;
acc_iData = iData;
acc_xp = xp;
}
accumulate += l;
}
if (l <= 0) {
/* rh: 20040215
* may happen due to "prev_data_use" optimization
*/
if (accumulate01) {
quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
accumulate01 = 0;
}
if (accumulate) {
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
accumulate = 0;
}
break; /* ends for-loop */
}
}
if (sfb <= sfbmax) {
iData += cod_info->width[sfb];
xp += cod_info->width[sfb];
j += cod_info->width[sfb];
}
}
if (accumulate) { /*last data part */
quantize_lines_xrpow(accumulate, istep, acc_xp, acc_iData);
accumulate = 0;
}
if (accumulate01) { /*last data part */
quantize_lines_xrpow_01(accumulate01, istep, acc_xp, acc_iData);
accumulate01 = 0;
}
}
/*************************************************************************/
/* ix_max */
/*************************************************************************/
static int
ix_max(const int *ix, const int *end)
{
int max1 = 0, max2 = 0;
do {
int const x1 = *ix++;
int const x2 = *ix++;
if (max1 < x1)
max1 = x1;
if (max2 < x2)
max2 = x2;
} while (ix < end);
if (max1 < max2)
max1 = max2;
return max1;
}
static int
count_bit_ESC(const int *ix, const int *const end, int t1, const int t2, unsigned int *const s)
{
/* ESC-table is used */
unsigned int const linbits = ht[t1].xlen * 65536u + ht[t2].xlen;
unsigned int sum = 0, sum2;
do {
unsigned int x = *ix++;
unsigned int y = *ix++;
if (x >= 15u) {
x = 15u;
sum += linbits;
}
if (y >= 15u) {
y = 15u;
sum += linbits;
}
x <<= 4u;
x += y;
sum += largetbl[x];
} while (ix < end);
sum2 = sum & 0xffffu;
sum >>= 16u;
if (sum > sum2) {
sum = sum2;
t1 = t2;
}
*s += sum;
return t1;
}
static int
count_bit_noESC(const int *ix, const int *end, int mx, unsigned int *s)
{
/* No ESC-words */
unsigned int sum1 = 0;
const uint8_t *const hlen1 = ht[1].hlen;
(void) mx;
do {
unsigned int const x0 = *ix++;
unsigned int const x1 = *ix++;
sum1 += hlen1[ x0+x0 + x1 ];
} while (ix < end);
*s += sum1;
return 1;
}
static const int huf_tbl_noESC[] = {
1, 2, 5, 7, 7, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13
};
static int
count_bit_noESC_from2(const int *ix, const int *end, int max, unsigned int *s)
{
int t1 = huf_tbl_noESC[max - 1];
/* No ESC-words */
const unsigned int xlen = ht[t1].xlen;
uint32_t const* table = (t1 == 2) ? &table23[0] : &table56[0];
unsigned int sum = 0, sum2;
do {
unsigned int const x0 = *ix++;
unsigned int const x1 = *ix++;
sum += table[ x0 * xlen + x1 ];
} while (ix < end);
sum2 = sum & 0xffffu;
sum >>= 16u;
if (sum > sum2) {
sum = sum2;
t1++;
}
*s += sum;
return t1;
}
inline static int
count_bit_noESC_from3(const int *ix, const int *end, int max, unsigned int * s)
{
int t1 = huf_tbl_noESC[max - 1];
/* No ESC-words */
unsigned int sum1 = 0;
unsigned int sum2 = 0;
unsigned int sum3 = 0;
const unsigned int xlen = ht[t1].xlen;
const uint8_t *const hlen1 = ht[t1].hlen;
const uint8_t *const hlen2 = ht[t1 + 1].hlen;
const uint8_t *const hlen3 = ht[t1 + 2].hlen;
int t;
do {
unsigned int x0 = *ix++;
unsigned int x1 = *ix++;
unsigned int x = x0 * xlen + x1;
sum1 += hlen1[x];
sum2 += hlen2[x];
sum3 += hlen3[x];
} while (ix < end);
t = t1;
if (sum1 > sum2) {
sum1 = sum2;
t++;
}
if (sum1 > sum3) {
sum1 = sum3;
t = t1 + 2;
}
*s += sum1;
return t;
}
/*************************************************************************/
/* choose table */
/*************************************************************************/
/*
Choose the Huffman table that will encode ix[begin..end] with
the fewest bits.
Note: This code contains knowledge about the sizes and characteristics
of the Huffman tables as defined in the IS (Table B.7), and will not work
with any arbitrary tables.
*/
static int count_bit_null(const int* ix, const int* end, int max, unsigned int* s)
{
(void) ix;
(void) end;
(void) max;
(void) s;
return 0;
}
typedef int (*count_fnc)(const int* ix, const int* end, int max, unsigned int* s);
static count_fnc count_fncs[] =
{ &count_bit_null
, &count_bit_noESC
, &count_bit_noESC_from2
, &count_bit_noESC_from2
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
, &count_bit_noESC_from3
};
static int
choose_table_nonMMX(const int *ix, const int *const end, int *const _s)
{
unsigned int* s = (unsigned int*)_s;
unsigned int max;
int choice, choice2;
max = ix_max(ix, end);
if (max <= 15) {
return count_fncs[max](ix, end, max, s);
}
/* try tables with linbits */
if (max > IXMAX_VAL) {
*s = LARGE_BITS;
return -1;
}
max -= 15u;
for (choice2 = 24; choice2 < 32; choice2++) {
if (ht[choice2].linmax >= max) {
break;
}
}
for (choice = choice2 - 8; choice < 24; choice++) {
if (ht[choice].linmax >= max) {
break;
}
}
return count_bit_ESC(ix, end, choice, choice2, s);
}
/*************************************************************************/
/* count_bit */
/*************************************************************************/
int
noquant_count_bits(lame_internal_flags const *const gfc,
gr_info * const gi, calc_noise_data * prev_noise)
{
SessionConfig_t const *const cfg = &gfc->cfg;
int bits = 0;
int i, a1, a2;
int const *const ix = gi->l3_enc;
i = Min(576, ((gi->max_nonzero_coeff + 2) >> 1) << 1);
if (prev_noise)
prev_noise->sfb_count1 = 0;
/* Determine count1 region */
for (; i > 1; i -= 2)
if (ix[i - 1] | ix[i - 2])
break;
gi->count1 = i;
/* Determines the number of bits to encode the quadruples. */
a1 = a2 = 0;
for (; i > 3; i -= 4) {
int x4 = ix[i-4];
int x3 = ix[i-3];
int x2 = ix[i-2];
int x1 = ix[i-1];
int p;
/* hack to check if all values <= 1 */
if ((unsigned int) (x4 | x3 | x2 | x1) > 1)
break;
p = ((x4 * 2 + x3) * 2 + x2) * 2 + x1;
a1 += t32l[p];
a2 += t33l[p];
}
bits = a1;
gi->count1table_select = 0;
if (a1 > a2) {
bits = a2;
gi->count1table_select = 1;
}
gi->count1bits = bits;
gi->big_values = i;
if (i == 0)
return bits;
if (gi->block_type == SHORT_TYPE) {
a1 = 3 * gfc->scalefac_band.s[3];
if (a1 > gi->big_values)
a1 = gi->big_values;
a2 = gi->big_values;
}
else if (gi->block_type == NORM_TYPE) {
assert(i <= 576); /* bv_scf has 576 entries (0..575) */
a1 = gi->region0_count = gfc->sv_qnt.bv_scf[i - 2];
a2 = gi->region1_count = gfc->sv_qnt.bv_scf[i - 1];
assert(a1 + a2 + 2 < SBPSY_l);
a2 = gfc->scalefac_band.l[a1 + a2 + 2];
a1 = gfc->scalefac_band.l[a1 + 1];
if (a2 < i)
gi->table_select[2] = gfc->choose_table(ix + a2, ix + i, &bits);
}
else {
gi->region0_count = 7;
/*gi->region1_count = SBPSY_l - 7 - 1; */
gi->region1_count = SBMAX_l - 1 - 7 - 1;
a1 = gfc->scalefac_band.l[7 + 1];
a2 = i;
if (a1 > a2) {
a1 = a2;
}
}
/* have to allow for the case when bigvalues < region0 < region1 */
/* (and region0, region1 are ignored) */
a1 = Min(a1, i);
a2 = Min(a2, i);
assert(a1 >= 0);
assert(a2 >= 0);
/* Count the number of bits necessary to code the bigvalues region. */
if (0 < a1)
gi->table_select[0] = gfc->choose_table(ix, ix + a1, &bits);
if (a1 < a2)
gi->table_select[1] = gfc->choose_table(ix + a1, ix + a2, &bits);
if (cfg->use_best_huffman == 2) {
gi->part2_3_length = bits;
best_huffman_divide(gfc, gi);
bits = gi->part2_3_length;
}
if (prev_noise) {
if (gi->block_type == NORM_TYPE) {
int sfb = 0;
while (gfc->scalefac_band.l[sfb] < gi->big_values) {
sfb++;
}
prev_noise->sfb_count1 = sfb;
}
}
return bits;
}
int
count_bits(lame_internal_flags const *const gfc,
const FLOAT * const xr, gr_info * const gi, calc_noise_data * prev_noise)
{
int *const ix = gi->l3_enc;
/* since quantize_xrpow uses table lookup, we need to check this first: */
FLOAT const w = (IXMAX_VAL) / IPOW20(gi->global_gain);
if (gi->xrpow_max > w)
return LARGE_BITS;
quantize_xrpow(xr, ix, IPOW20(gi->global_gain), gi, prev_noise);
if (gfc->sv_qnt.substep_shaping & 2) {
int sfb, j = 0;
/* 0.634521682242439 = 0.5946*2**(.5*0.1875) */
int const gain = gi->global_gain + gi->scalefac_scale;
const FLOAT roundfac = 0.634521682242439 / IPOW20(gain);
for (sfb = 0; sfb < gi->sfbmax; sfb++) {
int const width = gi->width[sfb];
assert(width >= 0);
if (!gfc->sv_qnt.pseudohalf[sfb]) {
j += width;
}
else {
int k;
for (k = j, j += width; k < j; ++k) {
ix[k] = (xr[k] >= roundfac) ? ix[k] : 0;
}
}
}
}
return noquant_count_bits(gfc, gi, prev_noise);
}
/***********************************************************************
re-calculate the best scalefac_compress using scfsi
the saved bits are kept in the bit reservoir.
**********************************************************************/
inline static void
recalc_divide_init(const lame_internal_flags * const gfc,
gr_info const *cod_info,
int const *const ix, int r01_bits[], int r01_div[], int r0_tbl[], int r1_tbl[])
{
int r0, r1, bigv, r0t, r1t, bits;
bigv = cod_info->big_values;
for (r0 = 0; r0 <= 7 + 15; r0++) {
r01_bits[r0] = LARGE_BITS;
}
for (r0 = 0; r0 < 16; r0++) {
int const a1 = gfc->scalefac_band.l[r0 + 1];
int r0bits;
if (a1 >= bigv)
break;
r0bits = 0;
r0t = gfc->choose_table(ix, ix + a1, &r0bits);
for (r1 = 0; r1 < 8; r1++) {
int const a2 = gfc->scalefac_band.l[r0 + r1 + 2];
if (a2 >= bigv)
break;
bits = r0bits;
r1t = gfc->choose_table(ix + a1, ix + a2, &bits);
if (r01_bits[r0 + r1] > bits) {
r01_bits[r0 + r1] = bits;
r01_div[r0 + r1] = r0;
r0_tbl[r0 + r1] = r0t;
r1_tbl[r0 + r1] = r1t;
}
}
}
}
inline static void
recalc_divide_sub(const lame_internal_flags * const gfc,
const gr_info * cod_info2,
gr_info * const gi,
const int *const ix,
const int r01_bits[], const int r01_div[], const int r0_tbl[], const int r1_tbl[])
{
int bits, r2, a2, bigv, r2t;
bigv = cod_info2->big_values;
for (r2 = 2; r2 < SBMAX_l + 1; r2++) {
a2 = gfc->scalefac_band.l[r2];
if (a2 >= bigv)
break;
bits = r01_bits[r2 - 2] + cod_info2->count1bits;
if (gi->part2_3_length <= bits)
break;
r2t = gfc->choose_table(ix + a2, ix + bigv, &bits);
if (gi->part2_3_length <= bits)
continue;
memcpy(gi, cod_info2, sizeof(gr_info));
gi->part2_3_length = bits;
gi->region0_count = r01_div[r2 - 2];
gi->region1_count = r2 - 2 - r01_div[r2 - 2];
gi->table_select[0] = r0_tbl[r2 - 2];
gi->table_select[1] = r1_tbl[r2 - 2];
gi->table_select[2] = r2t;
}
}
void
best_huffman_divide(const lame_internal_flags * const gfc, gr_info * const gi)
{
SessionConfig_t const *const cfg = &gfc->cfg;
int i, a1, a2;
gr_info cod_info2;
int const *const ix = gi->l3_enc;
int r01_bits[7 + 15 + 1];
int r01_div[7 + 15 + 1];
int r0_tbl[7 + 15 + 1];
int r1_tbl[7 + 15 + 1];
/* SHORT BLOCK stuff fails for MPEG2 */
if (gi->block_type == SHORT_TYPE && cfg->mode_gr == 1)
return;
memcpy(&cod_info2, gi, sizeof(gr_info));
if (gi->block_type == NORM_TYPE) {
recalc_divide_init(gfc, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
}
i = cod_info2.big_values;
if (i == 0 || (unsigned int) (ix[i - 2] | ix[i - 1]) > 1)
return;
i = gi->count1 + 2;
if (i > 576)
return;
/* Determines the number of bits to encode the quadruples. */
memcpy(&cod_info2, gi, sizeof(gr_info));
cod_info2.count1 = i;
a1 = a2 = 0;
assert(i <= 576);
for (; i > cod_info2.big_values; i -= 4) {
int const p = ((ix[i - 4] * 2 + ix[i - 3]) * 2 + ix[i - 2]) * 2 + ix[i - 1];
a1 += t32l[p];
a2 += t33l[p];
}
cod_info2.big_values = i;
cod_info2.count1table_select = 0;
if (a1 > a2) {
a1 = a2;
cod_info2.count1table_select = 1;
}
cod_info2.count1bits = a1;
if (cod_info2.block_type == NORM_TYPE)
recalc_divide_sub(gfc, &cod_info2, gi, ix, r01_bits, r01_div, r0_tbl, r1_tbl);
else {
/* Count the number of bits necessary to code the bigvalues region. */
cod_info2.part2_3_length = a1;
a1 = gfc->scalefac_band.l[7 + 1];
if (a1 > i) {
a1 = i;
}
if (a1 > 0)
cod_info2.table_select[0] =
gfc->choose_table(ix, ix + a1, (int *) &cod_info2.part2_3_length);
if (i > a1)
cod_info2.table_select[1] =
gfc->choose_table(ix + a1, ix + i, (int *) &cod_info2.part2_3_length);
if (gi->part2_3_length > cod_info2.part2_3_length)
memcpy(gi, &cod_info2, sizeof(gr_info));
}
}
static const int slen1_n[16] = { 1, 1, 1, 1, 8, 2, 2, 2, 4, 4, 4, 8, 8, 8, 16, 16 };
static const int slen2_n[16] = { 1, 2, 4, 8, 1, 2, 4, 8, 2, 4, 8, 2, 4, 8, 4, 8 };
const int slen1_tab[16] = { 0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4 };
const int slen2_tab[16] = { 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3 };
static void
scfsi_calc(int ch, III_side_info_t * l3_side)
{
unsigned int i;
int s1, s2, c1, c2;
int sfb;
gr_info *const gi = &l3_side->tt[1][ch];
gr_info const *const g0 = &l3_side->tt[0][ch];
for (i = 0; i < (sizeof(scfsi_band) / sizeof(int)) - 1; i++) {
for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
if (g0->scalefac[sfb] != gi->scalefac[sfb]
&& gi->scalefac[sfb] >= 0)
break;
}
if (sfb == scfsi_band[i + 1]) {
for (sfb = scfsi_band[i]; sfb < scfsi_band[i + 1]; sfb++) {
gi->scalefac[sfb] = -1;
}
l3_side->scfsi[ch][i] = 1;
}
}
s1 = c1 = 0;
for (sfb = 0; sfb < 11; sfb++) {
if (gi->scalefac[sfb] == -1)
continue;
c1++;
if (s1 < gi->scalefac[sfb])
s1 = gi->scalefac[sfb];
}
s2 = c2 = 0;
for (; sfb < SBPSY_l; sfb++) {
if (gi->scalefac[sfb] == -1)
continue;
c2++;