-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.c
executable file
·1496 lines (1321 loc) · 58 KB
/
decode.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
#include <string.h>
#include <assert.h>
#include "mppdec.h"
#include "codetable.h"
#include "codetable_data.h"
// InputBuff-Pufferüberläufe seltener abfangen, dafür etwas Rasen dahinter ???
// Funktionen mal wieder sauber durchnumerieren
// Calculate_New_V: Am Ende Werte gleich an allen notwendigen Stellen speichern
// 3DNow!-Code (Assembleranweisungen) besser voneinander unabhängig machen (reordern)
// Bitbedarf der einzelnen Bänder bestimmen, um RES/SFI/QQ-Bitbedarf gegeneinander abzustimmen
#define BITS (CHAR_BIT * sizeof(*InputBuff)) // Bits per InputBuff-Word
#define INC InputCnt = (InputCnt + 1) & IBUFMASK
Ibuf_t InputBuff [IBUFSIZE /* +128 */ ]; // enthält den Lese-Puffer
static Uint32_t mask [32 + 1];
size_t InputCnt; // aktuelle Position im Lese-Puffer
static Ibuf_t dword; // BITS Bit-Wort für Bitstrom-I/O
static Uint pos; // Position im aktuell decodierten BITS-bit-Wort
static size_t LastInputCnt = 0;
static Uint Wraps = 0;
/*
* Initialisieren aller Tabellen und Variablen
*/
void
Bitstream_init ( void )
{
Int i;
Uint32_t val;
InputCnt = (size_t)-1;
pos = BITS;
dword = 0; // Werte werden so initialisiert, daß beim nächsten Lesen von mindestens 1 bit das erste dword automatisch eingezogen wird
LastInputCnt = 0;
Wraps = 0;
for ( val = 0, i = 0; i <= 32; i++, val += val+1 )
mask [i] = val;
}
/*
* Lesen einer festen Anzahl von Bits aus dem Bitstrom. Es können garantiert 0...16 bit gelesen werden,
* bei auf 16 bit alignten Zugriffen bis 32 bit.
*/
Uint32_t
BitstreamLE_read ( Int bits )
{
Uint32_t ret;
ENTER(78);
ret = dword;
if ( (pos += bits) < BITS ) {
ret >>= BITS - pos;
}
else {
pos -= BITS;
INC; ReadLE32 ( dword, InputBuff+ InputCnt );
if ( pos > 0 ) {
ret <<= pos;
ret |= dword >> (BITS - pos);
}
}
ret &= mask [bits];
LEAVE(78);
REP (printf ( "read(%2u) = %u\n", bits, ret ));
return ret;
}
/*
* Schnellere Form für BitstreamLE_read(1)
*/
static Uint
BitstreamLE_read1 ( void )
{
Uint ret;
ENTER(93);
ret = (Uint)( dword >> ( BITS - ++pos) ) & 1;
if ( pos >= BITS ) {
INC; ReadLE32 ( dword, InputBuff+ InputCnt );
pos -= BITS;
}
LEAVE(93);
REP (printf ( "read( 1) = %u\n", ret ));
return ret;
}
/*
* Lesen von n bits (Restriktionen siehe BitstreamLE_read), ohne diese zu quitieren
*/
Uint32_t
BitstreamLE_preview ( Int bits )
{
Uint new_pos = pos + bits;
Uint32_t ret = dword;
Uint32_t tmp;
if ( new_pos < BITS ) {
ret >>= BITS - new_pos;
}
else if ( new_pos > BITS ) {
ret <<= new_pos - BITS;
ReadLE32 ( tmp, & InputBuff [(InputCnt+1) & IBUFMASK] );
ret |= tmp >> (2*BITS - new_pos);
}
return ret /* & mask[bits] */;
}
/*
* Decodiere Huffman-Kode, der maximal 14 bit lang sein darf
* Das Dekodieren erfolgt durch reines Scannen der Tabelle.
*/
static Int
HuffmanLE_Decode ( const Huffman_t* Table )
{
Uint32_t code;
Uint32_t tmp;
ENTER(79);
code = dword << pos;
if ( pos > BITS - 14 ) {
ReadLE32 ( tmp, & InputBuff [(InputCnt+1) & IBUFMASK] );
code |= tmp >> (BITS - pos);
}
while ( code < Table->Code )
Table++;
// Setze Bitstromposition ohne dummy-read
if ( (pos += Table->Length) >= BITS ) {
pos -= BITS;
INC; ReadLE32 ( dword, InputBuff+ InputCnt );
}
LEAVE(79);
REP (printf ( "decode() = %d\n", Table->Value ));
return Table->Value;
}
/*
* Decodiere Huffman-Kode, der maximal 14 bit lang sein darf
* Das Dekodieren erfolgt durch Grobpositionierung mittels Helpertabelle (tab,unused_bits),
* danach erfolgt ein weiteres Scannen, bis der Wert erreicht ist.
*/
static Int
HuffmanLE_Decode_faster ( const Huffman_t* Table, const Uint8_t* tab, Int unused_bits )
{
Uint32_t code;
Uint32_t tmp;
ENTER(93);
code = dword << pos;
if ( pos > BITS - 14 ) {
ReadLE32 ( tmp, & InputBuff [(InputCnt+1) & IBUFMASK] );
code |= tmp >> (BITS - pos);
}
Table += tab [(size_t)(code >> unused_bits) ];
while ( code < Table->Code )
Table++;
// Setze Bitstromposition ohne dummy-read
if ( (pos += Table->Length) >= BITS ) {
pos -= BITS;
INC; ReadLE32 ( dword, InputBuff+ InputCnt );
}
LEAVE(93);
REP (printf ( "decode() = %d\n", Table->Value ));
return Table->Value;
}
/*
* Decodiere Huffman-Kode, der maximal 16 bit lang sein darf
* Das Dekodieren erfolgt allein durch Tabellenlookup, damit nur für "kurze" Codes verwendbar,
* weil sonst riesige Tabellen notwendig werden
*/
static Int
HuffmanLE_Decode_fastest ( const Huffman_t* Table, const Uint8_t* tab, Int unused_bits )
{
Uint32_t code;
Uint32_t tmp;
ENTER(91);
code = dword << pos;
// ist die folgende Zeile optimal?
if ( pos > unused_bits + BITS - 32 ) {
ReadLE32 ( tmp, & InputBuff [(InputCnt+1) & IBUFMASK] );
code |= tmp >> (BITS - pos);
}
Table += tab [ (size_t) (code >> unused_bits) ];
// Setze Bitstromposition ohne dummy-read
if ( (pos += Table->Length) >= BITS ) {
pos -= BITS;
INC; ReadLE32 ( dword, InputBuff+ InputCnt );
}
LEAVE(91);
REP (printf ( "decode() = %d\n", Table->Value ));
return Table->Value;
}
#define HUFFMAN_DECODE_FASTER(a,b,c) HuffmanLE_Decode_faster ( (a), (b), 32-(c) )
#define HUFFMAN_DECODE_FASTEST(a,b,c) HuffmanLE_Decode_fastest ( (a), (b), 32-(c) )
#define Decode_DSCF() HUFFMAN_DECODE_FASTEST ( HuffDSCF, LUTDSCF, 6 )
/******************/
Uint32_t
BitstreamBE_read ( Int bits )
{
Uint32_t ret;
ENTER(78);
ret = dword;
if ( (pos += bits) < BITS ) {
ret >>= BITS - pos;
}
else {
pos -= BITS;
INC;
ReadBE32 ( dword, InputBuff + InputCnt );
if ( pos > 0 ) {
ret <<= pos;
ret |= dword >> (BITS - pos);
}
}
ret &= mask [bits];
LEAVE(78);
REP (printf ( "read(%2u) = %u\n", bits, ret ));
return ret;
}
/*
* Schnellere Form für BitstreamBE_read(1)
*/
static Uint
BitstreamBE_read1 ( void )
{
Uint ret;
ENTER(93);
ret = (Uint)( dword >> ( BITS - ++pos) ) & 1;
if ( pos >= BITS ) {
INC;
ReadBE32 ( dword, InputBuff + InputCnt );
pos -= BITS;
}
LEAVE(93);
REP (printf ( "read( 1) = %u\n", ret ));
return ret;
}
static Int
HuffDecode ( const HuffDecTable_t* T )
{
const HuffDecCode_t* p = T -> table;
Uint32_t code;
Uint32_t tmp;
ENTER(79);
code = dword << pos;
if ( pos > BITS - 29 ) { // !!!, dynamisch bestimmen
ReadBE32 ( tmp, & InputBuff [(InputCnt+1) & IBUFMASK] );
code |= tmp >> (BITS - pos);
}
while ( code < p -> pattern )
p++;
if ( (pos += p -> bits) >= BITS ) {
pos -= BITS;
INC; ReadBE32 ( dword, InputBuff + InputCnt ); // tmp benutzen!
}
LEAVE(79);
REP (printf ( "decode() = %d\n", p -> value ));
return p -> value;
}
static Int
HuffDecode_LUT ( const HuffDecTable_t* T )
{
const HuffDecCode_t* p = T -> table;
Uint32_t code;
Uint32_t tmp;
ENTER(93);
code = dword << pos;
if ( pos > BITS - 29 ) { // !!!, dynamisch bestimmen
ReadBE32 ( tmp, & InputBuff [(InputCnt+1) & IBUFMASK] );
code |= tmp >> (BITS - pos);
}
p += (T -> lut) [(size_t)(code >> T -> nolutbits) ];
while ( code < p -> pattern )
p++;
if ( (pos += p -> bits) >= BITS ) {
pos -= BITS;
INC; ReadBE32 ( dword, InputBuff + InputCnt ); // tmp benutzen!
}
LEAVE(93);
REP (printf ( "decode() = %d\n", p -> value ));
return p -> value;
}
static Int
HuffDecode_onlyLUT ( const HuffDecTable_t* T )
{
const HuffDecCode_t* p = T -> table;
Uint32_t code;
Uint32_t tmp;
ENTER(91);
#ifdef DEBUG // Test, ob Lookup-Table lang genug ist für HuffDecode_onlyLUT()
#endif
code = dword << pos;
if ( pos > BITS - 12 ) { // !!!
ReadBE32 ( tmp, & InputBuff [(InputCnt+1) & IBUFMASK] );
code |= tmp >> (BITS - pos);
}
p += T->lut [ (size_t) (code >> T -> nolutbits) ];
if ( (pos += p -> bits) >= BITS ) {
pos -= BITS;
INC; ReadBE32 ( dword, InputBuff + InputCnt ); // tmp benutzen!
}
LEAVE(91);
REP (printf ( "decode() = %d\n", p -> value ));
return p -> value;
}
/******************/
Ulong
BitsRead ( void )
{
if ( LastInputCnt > InputCnt )
Wraps++;
LastInputCnt = InputCnt;
return ((Ulong)Wraps*IBUFSIZE + InputCnt) * BITS + pos;
}
#include "dump.c"
Ulong __x[8];
#define BITPOS(x) __x[x] = BitsRead ()
/*
* Höhere Auflösungen (ab Stufe 8) sind nicht mehr huffmankodiert, Anzahl der Bits, die dann direkt gelesen werden
* Bits pro Sample für gewählte Auflösungsstufe, nur für die hochaufösenden ohne Huffman-Kodierung
*/
/******************************************************************************************/
/****************************************** SV 6 ******************************************/
/******************************************************************************************/
static int
Read_Bitstream_SV6 ( Uint MS_bits )
{
Int Band;
Uint k;
const Huffman_t* Table;
const Huffman_t* xL;
const Huffman_t* xR;
Int Max_Used_Band = -1; // ????????????? vorher 0 ??????????????
pack_t SCFI [2] [32];
ENTER(6);
/********* Lese Auflösung und LR/MS für alle Subbänder und bestimme letztes Subband *********************/
BITPOS(0);
for ( Band = 0; Band <= Max_Band; Band++ ) {
Table = Region [Band];
Res[0][Band] = Q_res[Band][Bitrate <= 128 ? HuffmanLE_Decode(Table) : (Int) BitstreamLE_read(Q_bit[Band])];
Res[1][Band] = 0;
// Nicht lesen für IS für Bänder ab MinBand+1
if ( !IS_used || Band < Min_Band ) {
MS_Band [Band] = 0;
if ( MS_bits )
MS_Band [Band] = BitstreamLE_read1 ();
Res[1][Band] = Q_res[Band][Bitrate <= 128 ? HuffmanLE_Decode(Table) : (Int) BitstreamLE_read(Q_bit[Band])];
}
// Bestimme letztes benutztes Subband (folgende Operationen werden nur noch bis zu diesem ausgeführt)
if ( Res[0][Band] || Res[1][Band] )
Max_Used_Band = Band;
}
/********* Lese verwendetes Scalebandfactor-Splitting der 36 Samples pro Subband und Werteaddressierung (abs./rel.) */
BITPOS(1);
for ( Band = 0; Band <= Max_Used_Band; Band++ ) {
if ( Res[0][Band] )
SCFI[0][Band] = HuffmanLE_Decode (SCFI_Bundle);
if ( Res[1][Band] || (Res[0][Band] && IS_used && Band >= Min_Band) )
SCFI[1][Band] = HuffmanLE_Decode (SCFI_Bundle);
}
/********* Lese Scalefaktors für alle Subbänder dreimal für jeweils 12 Samples **************************/
BITPOS(2);
Table = DSCF_Entropie;
for ( Band = 0; Band <= Max_Used_Band; Band++ ) {
if ( Res[0][Band] ) {
switch ( SCFI[0][Band] ) {
case 0: // ohne Differential SCF
SCF_Index[0][Band][0] = (Int) BitstreamLE_read(6);
SCF_Index[0][Band][1] = (Int) BitstreamLE_read(6);
SCF_Index[0][Band][2] = (Int) BitstreamLE_read(6);
break;
case 2:
SCF_Index[0][Band][0] = (Int) BitstreamLE_read(6);
SCF_Index[0][Band][1] =
SCF_Index[0][Band][2] = (Int) BitstreamLE_read(6);
break;
case 4:
SCF_Index[0][Band][0] =
SCF_Index[0][Band][1] = (Int) BitstreamLE_read(6);
SCF_Index[0][Band][2] = (Int) BitstreamLE_read(6);
break;
case 6:
SCF_Index[0][Band][0] =
SCF_Index[0][Band][1] =
SCF_Index[0][Band][2] = (Int) BitstreamLE_read(6);
break;
case 1: // mit Differential SCF
SCF_Index[0][Band][0] = SCF_Index[0][Band][2] + HuffmanLE_Decode(Table);
SCF_Index[0][Band][1] = SCF_Index[0][Band][0] + HuffmanLE_Decode(Table);
SCF_Index[0][Band][2] = SCF_Index[0][Band][1] + HuffmanLE_Decode(Table);
break;
default:
assert (0);
case 3:
SCF_Index[0][Band][0] = SCF_Index[0][Band][2] + HuffmanLE_Decode(Table);
SCF_Index[0][Band][1] =
SCF_Index[0][Band][2] = SCF_Index[0][Band][0] + HuffmanLE_Decode(Table);
break;
case 5:
SCF_Index[0][Band][0] =
SCF_Index[0][Band][1] = SCF_Index[0][Band][2] + HuffmanLE_Decode(Table);
SCF_Index[0][Band][2] = SCF_Index[0][Band][1] + HuffmanLE_Decode(Table);
break;
case 7:
SCF_Index[0][Band][0] =
SCF_Index[0][Band][1] =
SCF_Index[0][Band][2] = SCF_Index[0][Band][2] + HuffmanLE_Decode(Table);
break;
}
}
if ( Res[1][Band] || (Res[0][Band] && IS_used && Band >= Min_Band) ) {
switch ( SCFI[1][Band] ) {
case 0:
SCF_Index[1][Band][0] = (Int) BitstreamLE_read(6);
SCF_Index[1][Band][1] = (Int) BitstreamLE_read(6);
SCF_Index[1][Band][2] = (Int) BitstreamLE_read(6);
break;
case 2:
SCF_Index[1][Band][0] = (Int) BitstreamLE_read(6);
SCF_Index[1][Band][1] =
SCF_Index[1][Band][2] = (Int) BitstreamLE_read(6);
break;
case 4:
SCF_Index[1][Band][0] =
SCF_Index[1][Band][1] = (Int) BitstreamLE_read(6);
SCF_Index[1][Band][2] = (Int) BitstreamLE_read(6);
break;
case 6:
SCF_Index[1][Band][0] =
SCF_Index[1][Band][1] =
SCF_Index[1][Band][2] = (Int) BitstreamLE_read(6);
break;
case 1:
SCF_Index[1][Band][0] = SCF_Index[1][Band][2] + HuffmanLE_Decode(Table);
SCF_Index[1][Band][1] = SCF_Index[1][Band][0] + HuffmanLE_Decode(Table);
SCF_Index[1][Band][2] = SCF_Index[1][Band][1] + HuffmanLE_Decode(Table);
break;
default:
assert (0);
case 3:
SCF_Index[1][Band][0] = SCF_Index[1][Band][2] + HuffmanLE_Decode(Table);
SCF_Index[1][Band][1] =
SCF_Index[1][Band][2] = SCF_Index[1][Band][0] + HuffmanLE_Decode(Table);
break;
case 5:
SCF_Index[1][Band][0] =
SCF_Index[1][Band][1] = SCF_Index[1][Band][2] + HuffmanLE_Decode(Table);
SCF_Index[1][Band][2] = SCF_Index[1][Band][1] + HuffmanLE_Decode(Table);
break;
case 7:
SCF_Index[1][Band][0] =
SCF_Index[1][Band][1] =
SCF_Index[1][Band][2] = SCF_Index[1][Band][2] + HuffmanLE_Decode(Table);
break;
}
}
}
/********* Lese die quantisierten Samples pro Subband (ohne Offsets, d.h. Werte liegen nullsymmetrisch) */
BITPOS(3);
for ( Band = 0; Band <= Max_Used_Band; Band++ ) {
xL = Entropie [Res[0][Band]];
xR = Entropie [Res[1][Band]];
if ( xL != NULL || xR != NULL )
for ( k = 0; k < 36; k++ ) {
if ( xL != NULL )
QQ [0] [Band] [k] = HuffmanLE_Decode (xL);
if ( xR != NULL )
QQ [1] [Band] [k] = HuffmanLE_Decode (xR);
}
if ( Res[0][Band] >= 8 || Res[1][Band] >= 8 )
for ( k = 0; k < 36; k++ ) {
if ( Res[0][Band] >= 8 )
QQ [0] [Band] [k] = (Int) BitstreamLE_read (Res[0][Band]-1) - Dc7[Res[0][Band]];
if ( Res[1][Band] >= 8 )
QQ [1] [Band] [k] = (Int) BitstreamLE_read (Res[1][Band]-1) - Dc7[Res[1][Band]];
}
}
BITPOS(4);
if ( DUMPSELECT > 0 )
Dump ( Max_Used_Band, 2, MS_Band, Res, SCF_Index, QQ, 0x06, __x );
LEAVE(6);
return Max_Used_Band;
}
#if 0
static void
CalculateTNS ( Float TNS [36], const res_t Res[], const quant_t Q [] [36], int Band ) // For a non-shaped output the vector should be all 65536 / 5 = 13170.2
{
int i;
int j;
Float Sum;
for ( i = 0; i < 36; i++ )
TNS [i] = 3.;
for ( j = (Band+1)/2; j-- > 0; ) {
do {
Q--;
if (Band-- == 0) goto cont;
} while ( *--Res <= 0 );
for ( i = 0; i < 36; i++ )
TNS [i] += Q [0] [i] * Q [0] [i];
}
cont:
for ( j = 0; j < 3; j++ ) {
Sum = 0;
for ( i = 0; i < 12; i++ )
Sum += TNS [12*j + i];
Sum = sqrt (12. / Sum) * 13170.2;
for ( i = 0; i < 12; i++ )
TNS [12*j + i] = sqrt (TNS [12*j + i]) * Sum;
}
}
#endif
/******************************************************************************************/
/****************************************** SV 7 ******************************************/
/******************************************************************************************/
static int
Read_Bitstream_SV7 ( Uint MS_bits )
{
static Schar tab30 [3*3*3] = { -1, 0,+1,-1, 0,+1,-1, 0,+1,-1, 0,+1,-1, 0,+1,-1, 0,+1,-1, 0,+1,-1, 0,+1,-1, 0,+1 };
static Schar tab31 [3*3*3] = { -1,-1,-1, 0, 0, 0,+1,+1,+1,-1,-1,-1, 0, 0, 0,+1,+1,+1,-1,-1,-1, 0, 0, 0,+1,+1,+1 };
static Schar tab32 [3*3*3] = { -1,-1,-1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0,+1,+1,+1,+1,+1,+1,+1,+1,+1 };
static Schar tab50 [5*5 ] = { -2,-1, 0,+1,+2,-2,-1, 0,+1,+2,-2,-1, 0,+1,+2,-2,-1, 0,+1,+2,-2,-1, 0,+1,+2 };
static Schar tab51 [5*5 ] = { -2,-2,-2,-2,-2,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0,+1,+1,+1,+1,+1,+2,+2,+2,+2,+2 };
// Float TNS [CH] [36];
Int Band;
Uint k;
quant_t* pp;
const Huffman_t* Table;
Int diff;
Uint idx;
Uint32_t tmp;
Int Max_Used_Band = -1;
pack_t SCFI [2] [32];
ENTER(7);
/********* Lese Auflösung und LR/MS für Subband 0 *******************************************************/
BITPOS(0);
Res[0][0] = (Int) BitstreamLE_read(4);
Res[1][0] = (Int) BitstreamLE_read(4);
MS_Band [0] = 0;
if ( Res[0][0] || Res[1][0] ) {
Max_Used_Band = 0; // Bestimme letztes benutztes Subband (folgende Operationen werden nur noch bis zu diesem ausgeführt)
if ( MS_bits )
MS_Band [0] = BitstreamLE_read1 ();
}
/********* Lese Auflösung und LR/MS für folgende Subbänder und bestimme letztes Subband *****************/
Table = HuffHdr;
for ( Band = 1; Band <= Max_Band; Band++ ) {
diff = HuffmanLE_Decode (Table);
Res[0][Band] = diff != 4 ? Res[0][Band-1] + diff : (Int) BitstreamLE_read(4);
diff = HuffmanLE_Decode (Table);
Res[1][Band] = diff != 4 ? Res[1][Band-1] + diff : (Int) BitstreamLE_read(4);
MS_Band [Band] = 0;
if ( Res[0][Band] || Res[1][Band] ) {
Max_Used_Band = Band; // Bestimme letztes benutztes Subband (folgende Operationen werden nur noch bis zu diesem ausgeführt)
if ( MS_bits )
MS_Band [Band] = BitstreamLE_read1 ();
}
}
/********* Lese verwendetes Scalebandfactor-Splitting der 36 Samples pro Subband ************************/
BITPOS(1);
Table = HuffSCFI;
for ( Band = 0; Band <= Max_Used_Band; Band++ ) {
if ( Res[0][Band] )
SCFI[0][Band] = HuffmanLE_Decode (Table);
if ( Res[1][Band] )
SCFI[1][Band] = HuffmanLE_Decode (Table);
}
/********* Lese Scalefaktors für alle Subbänder dreimal für jeweils 12 Samples **************************/
BITPOS(2);
Table = HuffDSCF;
for ( Band = 0; Band <= Max_Used_Band; Band++ ) {
if ( Res[0][Band] ) {
switch ( SCFI[0][Band] ) {
case 0:
diff = Decode_DSCF ();
SCF_Index[0][Band][0] = diff!=8 ? SCF_Index[0][Band][2] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[0][Band][1] = diff!=8 ? SCF_Index[0][Band][0] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[0][Band][2] = diff!=8 ? SCF_Index[0][Band][1] + diff : (Int) BitstreamLE_read(6);
break;
case 1:
diff = Decode_DSCF ();
SCF_Index[0][Band][0] = diff!=8 ? SCF_Index[0][Band][2] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[0][Band][1] =
SCF_Index[0][Band][2] = diff!=8 ? SCF_Index[0][Band][0] + diff : (Int) BitstreamLE_read(6);
break;
case 2:
diff = Decode_DSCF ();
SCF_Index[0][Band][0] =
SCF_Index[0][Band][1] = diff!=8 ? SCF_Index[0][Band][2] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[0][Band][2] = diff!=8 ? SCF_Index[0][Band][1] + diff : (Int) BitstreamLE_read(6);
break;
default:
assert (0);
case 3:
diff = Decode_DSCF ();
SCF_Index[0][Band][0] =
SCF_Index[0][Band][1] =
SCF_Index[0][Band][2] = diff!=8 ? SCF_Index[0][Band][2] + diff : (Int) BitstreamLE_read(6);
break;
}
}
if ( Res[1][Band] ) {
switch ( SCFI[1][Band] ) {
case 0:
diff = Decode_DSCF ();
SCF_Index[1][Band][0] = diff!=8 ? SCF_Index[1][Band][2] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[1][Band][1] = diff!=8 ? SCF_Index[1][Band][0] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[1][Band][2] = diff!=8 ? SCF_Index[1][Band][1] + diff : (Int) BitstreamLE_read(6);
break;
case 1:
diff = Decode_DSCF ();
SCF_Index[1][Band][0] = diff!=8 ? SCF_Index[1][Band][2] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[1][Band][1] =
SCF_Index[1][Band][2] = diff!=8 ? SCF_Index[1][Band][0] + diff : (Int) BitstreamLE_read(6);
break;
case 2:
diff = Decode_DSCF ();
SCF_Index[1][Band][0] =
SCF_Index[1][Band][1] = diff!=8 ? SCF_Index[1][Band][2] + diff : (Int) BitstreamLE_read(6);
diff = Decode_DSCF ();
SCF_Index[1][Band][2] = diff!=8 ? SCF_Index[1][Band][1] + diff : (Int) BitstreamLE_read(6);
break;
default:
assert (0);
case 3:
diff = Decode_DSCF ();
SCF_Index[1][Band][0] =
SCF_Index[1][Band][1] =
SCF_Index[1][Band][2] = diff!=8 ? SCF_Index[1][Band][2] + diff : (Int) BitstreamLE_read(6);
break;
}
}
}
/********* Lese die quantisierten Samples pro Subband (ohne Offsets, d.h. Werte liegen nullsymmetrisch) */
BITPOS(3);
for ( Band = 0; Band <= Max_Used_Band; Band++ ) {
pp = QQ [0] [Band];
switch ( Res[0][Band] ) {
case -2:
for ( k = 0; k < 36; k++ )
*pp++ = 0;
break;
case -1:
#if 0
if ( Res[0][Band-1] != -1 )
CalculateTNS ( TNS[0], Res[0] + Band, (const quant_t(*)[36]) pp, Band );
tmp = random_int ();
for (k=0; k<36/2; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2)) * TNS [0][k];
tmp = random_int ();
for (k=36/2; k<36; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2)) * TNS [0][k];
#elif 0
tmp = random_int ();
for (k=0; k<36/2; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2));
tmp = random_int ();
for (k=36/2; k<36; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2));
#else
for (k=0; k<36; k++ ) {
tmp = random_int ();
*pp++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
}
#endif
break;
case 0:
// Subband samples are not used in this case, see Requant Routines
break;
case 1:
if ( BitstreamLE_read1 () )
for (k=0; k<36/3; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ1[1], LUT1_1, 9 );
*pp++ = tab30[idx];
*pp++ = tab31[idx];
*pp++ = tab32[idx];
}
else
for (k=0; k<36/3; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ1[0], LUT1_0, 6 );
*pp++ = tab30[idx];
*pp++ = tab31[idx];
*pp++ = tab32[idx];
}
break;
case 2:
if ( BitstreamLE_read1 () )
for (k=0; k<36/2; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ2[1], LUT2_1, 10 );
*pp++ = tab50[idx];
*pp++ = tab51[idx];
}
else
for (k=0; k<36/2; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ2[0], LUT2_0, 7 );
*pp++ = tab50[idx];
*pp++ = tab51[idx];
}
break;
case 3:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ3[1], LUT3_1, 5 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ3[0], LUT3_0, 4 );
break;
case 4:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ4[1], LUT4_1, 5 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ4[0], LUT4_0, 4 );
break;
case 5:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ5[1], LUT5_1, 8 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ5[0], LUT5_0, 6 );
break;
case 6:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTER ( HuffQ6[1], LUT6_1, 7 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ6[0], LUT6_0, 7 );
break;
case 7:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTER ( HuffQ7[1], LUT7_1, 8 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ7[0], LUT7_0, 8 );
break;
case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
tmp = Dc7 [ Res[0][Band] ];
for ( k = 0; k < 36; k++ )
*pp++ = (Int) BitstreamLE_read (Res[0][Band]-1) - tmp;
break;
default:
return -1;
}
pp = QQ [1] [Band];
switch ( Res[1][Band] ) {
case -2:
for ( k = 0; k < 36; k++ )
*pp++ = QQ [0] [Band] [k];
break;
case -1:
#if 0
if ( Res[1][Band-1] != -1 )
CalculateTNS ( TNS[1], Res[1] + Band, (const quant_t(*)[36]) pp, Band );
tmp = random_int ();
for (k=0; k<36/2; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2)) * TNS [1][k];
tmp = random_int ();
for (k=36/2; k<36; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2)) * TNS [1][k];
#elif 0
tmp = random_int ();
for (k=0; k<36/2; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2));
tmp = random_int ();
for (k=36/2; k<36; k++, tmp >>= 1)
*pp++ = (int)(1 - (tmp & 2));
#else
for (k=0; k<36; k++ ) {
tmp = random_int ();
*pp++ = ((tmp >> 24) & 0xFF) + ((tmp >> 16) & 0xFF) + ((tmp >> 8) & 0xFF) + ((tmp >> 0) & 0xFF) - 510;
}
#endif
break;
case 0:
// Subband samples are not used in this case, see Requant Routines
break;
case 1:
if ( BitstreamLE_read1 () )
for (k=0; k<36/3; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ1[1], LUT1_1, 9 );
*pp++ = tab30[idx];
*pp++ = tab31[idx];
*pp++ = tab32[idx];
}
else
for (k=0; k<36/3; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ1[0], LUT1_0, 6 );
*pp++ = tab30[idx];
*pp++ = tab31[idx];
*pp++ = tab32[idx];
}
break;
case 2:
if ( BitstreamLE_read1 () )
for (k=0; k<36/2; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ2[1], LUT2_1, 10 );
*pp++ = tab50[idx];
*pp++ = tab51[idx];
}
else
for (k=0; k<36/2; k++) {
idx = HUFFMAN_DECODE_FASTEST ( HuffQ2[0], LUT2_0, 7 );
*pp++ = tab50[idx];
*pp++ = tab51[idx];
}
break;
case 3:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ3[1], LUT3_1, 5 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ3[0], LUT3_0, 4 );
break;
case 4:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ4[1], LUT4_1, 5 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ4[0], LUT4_0, 4 );
break;
case 5:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ5[1], LUT5_1, 8 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ5[0], LUT5_0, 6 );
break;
case 6:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTER ( HuffQ6[1], LUT6_1, 7 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ6[0], LUT6_0, 7 );
break;
case 7:
if ( BitstreamLE_read1 () )
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTER ( HuffQ7[1], LUT7_1, 8 );
else
for ( k = 0; k < 36; k++ )
*pp++ = HUFFMAN_DECODE_FASTEST ( HuffQ7[0], LUT7_0, 8 );
break;
case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17:
tmp = Dc7 [ Res[1][Band] ];
for ( k = 0; k < 36; k++ )
*pp++ = (Int) BitstreamLE_read (Res[1][Band]-1) - tmp;
break;
default:
return -1;
}
}
BITPOS(4);
if ( DUMPSELECT > 0 )
Dump ( Max_Used_Band, 2, MS_Band, Res, SCF_Index, QQ, 0x07, __x );
LEAVE(7);