-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.c
executable file
·712 lines (653 loc) · 27.3 KB
/
tools.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
/*
* Clear Vocal Detection functions
*
* (C) Andree Buschmann 2000, Frank Klemm 2001,02. All rights reserved.
*
* Principles:
*
* History:
* ca. 1998 created
* 2002
*
* Global functions:
* -
*
* TODO:
* -
*/
/*
* A list of different mixed tools
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Requantize_MidSideStereo(), Requantize_IntensityStereo()
* Requantisation of quantized samples for synthesis filter
* Resort_HuffTable(), Make_HuffTable(), Make_LookupTable()
* Generating and sorting Huffman tables, making fast lookup tables
*/
#include <string.h>
#include <errno.h>
#include "mppdec.h"
#if defined HAVE_INCOMPLETE_READ && FILEIO != 1
size_t
complete_read ( int fd, void* dest, size_t bytes )
{
size_t bytesread = 0;
size_t ret;
while ( bytes > 0 ) {
#if defined _WIN32 && defined USE_HTTP && !defined MPP_ENCODER
ret = fd & 0x4000 ? recv ( fd & 0x3FFF, dest, bytes, 0) : read ( fd, dest, bytes );
#else
ret = read ( fd, dest, bytes );
#endif
if ( ret == 0 || ret == (size_t)-1 )
break;
dest = (void*)(((char*)dest) + ret);
bytes -= ret;
bytesread += ret;
}
return bytesread;
}
#endif
#ifndef MPP_ENCODER
/*
* This is the main requantisation routine which does the following things:
*
* - rescaling the quantized values (int) to their original value (float)
* - recalculating both stereo channels for MS stereo
*
* See also: Requantize_IntensityStereo()
*
* For performance reasons all cases are programmed separately and the code
* is unrolled.
*
* Input is:
* - Stop_Band:
* the last band using MS or LR stereo
* - used_MS[Band]:
* MS or LR stereo flag for every band (0...Stop_Band), Value is 1
* for MS and 0 for LR stereo.
* - Res [0..CH-1] [Band]:
* Quantisation resolution for every band (0...Stop_Band) and
* channels (L, R). Value is 0...17.
* - SCF_Index [0..CH-1] [Band] [3]:
* Scale factor for every band (0...Stop_Band), subframe (0...2)
* and channel (L, R).
* - QQ [0..CH-1] [Band] [36]:
* 36 subband samples for every band (0...Stop_Band) and channel (L, R).
* - SCF[64], Cc7[18], Dc7[18]:
* Lookup tables for Scale factor and Quantisation resolution.
*
* Output is:
* - YY [0]: Left channel subband signals
* - YY [1]: Right channel subband signals
*
* These signals are used for the synthesis filter in the synth*.[ch]
* files to generate the PCM output signal.
*/
static void
Requantize_MidSideStereo_SV7 ( Int Stop_Band, const Bool_t* used_MS )
{
Int Band; // 0...Stop_Band
Uint k; // 0...35
Float ML;
Float MR;
Float mid;
Float side;
ENTER(162);
for ( Band = 0; Band <= Stop_Band; Band++ ) {
///////////////////////////////////////////////////////////////////////////////////
if ( used_MS[Band] ) { // MidSide coded: left channel contains Mid signal, right channel Side signal
if ( Res[0][Band] ) {
if ( Res[1][Band] < -1 ) {
k = 0;
ML = +SCF[SCF_Index[0][Band][0]] * Cc7[Res[0][Band]];
MR = -SCF[SCF_Index[1][Band][0]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = +SCF[SCF_Index[0][Band][1]] * Cc7[Res[0][Band]];
MR = -SCF[SCF_Index[1][Band][1]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = +SCF[SCF_Index[0][Band][2]] * Cc7[Res[0][Band]];
MR = -SCF[SCF_Index[1][Band][2]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
else if ( Res[1][Band] != 0 ) { // M!=0, S!=0
k = 0;
ML = SCF[SCF_Index[0][Band][0]] * Cc7[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][0]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = (mid = QQ [0] [Band] [k] * ML) - (side = QQ [1] [Band] [k] * MR);
YY [0][k][Band] = mid + side;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]] * Cc7[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][1]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = (mid = QQ [0] [Band] [k] * ML) - (side = QQ [1] [Band] [k] * MR);
YY [0][k][Band] = mid + side;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]] * Cc7[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][2]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = (mid = QQ [0] [Band] [k] * ML) - (side = QQ [1] [Band] [k] * MR);
YY [0][k][Band] = mid + side;
} while (++k < 36);
}
else { // M!=0, S=0
k = 0;
ML = SCF[SCF_Index[0][Band][0]] * Cc7[Res[0][Band]];
do {
YY [1][k][Band] =
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]] * Cc7[Res[0][Band]];
do {
YY [1][k][Band] =
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]] * Cc7[Res[0][Band]];
do {
YY [1][k][Band] =
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
}
else {
if ( Res[1][Band] < -1 ) {
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
else if ( Res[1][Band] != 0 ) { // M==0, S!=0
k = 0;
ML = SCF[SCF_Index[1][Band][0]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = - (
YY [0][k][Band] = QQ [1] [Band] [k] * ML );
} while (++k < 12);
ML = SCF[SCF_Index[1][Band][1]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = - (
YY [0][k][Band] = QQ [1] [Band] [k] * ML );
} while (++k < 24);
ML = SCF[SCF_Index[1][Band][2]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = - (
YY [0][k][Band] = QQ [1] [Band] [k] * ML );
} while (++k < 36);
}
else { // M==0, S==0
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
}
}
else { // Left/Right coded: left channel contains left, right the right signal
if ( Res[0][Band] ) {
if ( Res[1][Band] < -1 ) {
k = 0;
ML = +SCF[SCF_Index[0][Band][0]] * Cc7[Res[0][Band]];
MR = +SCF[SCF_Index[1][Band][0]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = +SCF[SCF_Index[0][Band][1]] * Cc7[Res[0][Band]];
MR = +SCF[SCF_Index[1][Band][1]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = +SCF[SCF_Index[0][Band][2]] * Cc7[Res[0][Band]];
MR = +SCF[SCF_Index[1][Band][2]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
else if ( Res[1][Band] != 0 ) { // L!=0, R!=0
k = 0;
ML = SCF[SCF_Index[0][Band][0]] * Cc7[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][0]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]] * Cc7[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][1]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]] * Cc7[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][2]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
else { // L!=0, R==0
k = 0;
ML = SCF[SCF_Index[0][Band][0]] * Cc7[Res[0][Band]];
do {
YY [1][k][Band] = 0.f;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]] * Cc7[Res[0][Band]];
do {
YY [1][k][Band] = 0.f;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]] * Cc7[Res[0][Band]];
do {
YY [1][k][Band] = 0.f;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
}
else {
if ( Res[1][Band] < -1 ) {
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
else if ( Res[1][Band] != 0 ) { // L==0, R!=0
k = 0;
MR = SCF[SCF_Index[1][Band][0]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = 0.f;
} while (++k < 12);
MR = SCF[SCF_Index[1][Band][1]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = 0.f;
} while (++k < 24);
MR = SCF[SCF_Index[1][Band][2]] * Cc7[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = 0.f;
} while (++k < 36);
}
else { // L==0, R==0
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
}
LEAVE(162);
return;
}
static void
Requantize_MidSideStereo_SV7F ( Int Stop_Band, const Bool_t* used_MS )
{
Int Band; // 0...Stop_Band
Uint k; // 0...35
Float ML;
Float MR;
Float mid;
Float side;
ENTER(162);
for ( Band = 0; Band <= Stop_Band; Band++ ) {
if ( used_MS[Band] ) { // MidSide coded: left channel contains Mid signal, right channel Side signal
if ( Res[0][Band] ) {
if ( Res[1][Band] < 0 ) {
k = 0;
ML = +SCF[SCF_Index[0][Band][0]] * Cc8[Res[0][Band]];
MR = -SCF[SCF_Index[1][Band][0]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = +SCF[SCF_Index[0][Band][1]] * Cc8[Res[0][Band]];
MR = -SCF[SCF_Index[1][Band][1]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = +SCF[SCF_Index[0][Band][2]] * Cc8[Res[0][Band]];
MR = -SCF[SCF_Index[1][Band][2]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
else if ( Res[1][Band] != 0 ) { // M!=0, S!=0
k = 0;
ML = SCF[SCF_Index[0][Band][0]-2] * Cc8[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][0]-2] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = (mid = QQ [0] [Band] [k] * ML) - (side = QQ [1] [Band] [k] * MR);
YY [0][k][Band] = mid + side;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]-2] * Cc8[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][1]-2] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = (mid = QQ [0] [Band] [k] * ML) - (side = QQ [1] [Band] [k] * MR);
YY [0][k][Band] = mid + side;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]-2] * Cc8[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][2]-2] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = (mid = QQ [0] [Band] [k] * ML) - (side = QQ [1] [Band] [k] * MR);
YY [0][k][Band] = mid + side;
} while (++k < 36);
}
else { // M!=0, S=0
k = 0;
ML = SCF[SCF_Index[0][Band][0]-2] * Cc8[Res[0][Band]];
do {
YY [1][k][Band] =
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]-2] * Cc8[Res[0][Band]];
do {
YY [1][k][Band] =
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]-2] * Cc8[Res[0][Band]];
do {
YY [1][k][Band] =
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
}
else {
if ( Res[1][Band] < 0 ) {
for (k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
else if ( Res[1][Band] != 0 ) { // M==0, S!=0
k = 0;
ML = SCF[SCF_Index[1][Band][0]-2] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = - (
YY [0][k][Band] = QQ [1] [Band] [k] * ML );
} while (++k < 12);
ML = SCF[SCF_Index[1][Band][1]-2] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = - (
YY [0][k][Band] = QQ [1] [Band] [k] * ML );
} while (++k < 24);
ML = SCF[SCF_Index[1][Band][2]-2] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = - (
YY [0][k][Band] = QQ [1] [Band] [k] * ML );
} while (++k < 36);
}
else { // M==0, S==0
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
else { // Left/Right coded: left channel contains left, right the right signal
if ( Res[0][Band] ) {
if ( Res[1][Band] < 0 ) {
k = 0;
ML = +SCF[SCF_Index[0][Band][0]] * Cc8[Res[0][Band]];
MR = +SCF[SCF_Index[1][Band][0]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = +SCF[SCF_Index[0][Band][1]] * Cc8[Res[0][Band]];
MR = +SCF[SCF_Index[1][Band][1]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = +SCF[SCF_Index[0][Band][2]] * Cc8[Res[0][Band]];
MR = +SCF[SCF_Index[1][Band][2]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
else if ( Res[1][Band] != 0 ) { // L!=0, R!=0
k = 0;
ML = SCF[SCF_Index[0][Band][0]] * Cc8[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][0]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]] * Cc8[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][1]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]] * Cc8[Res[0][Band]];
MR = SCF[SCF_Index[1][Band][2]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
else { // L!=0, R==0
k = 0;
ML = SCF[SCF_Index[0][Band][0]] * Cc8[Res[0][Band]];
do {
YY [1][k][Band] = 0.f;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]] * Cc8[Res[0][Band]];
do {
YY [1][k][Band] = 0.f;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]] * Cc8[Res[0][Band]];
do {
YY [1][k][Band] = 0.f;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
}
else {
if ( Res[1][Band] < 0 ) {
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
else if ( Res[1][Band] != 0 ) { // L==0, R!=0
k = 0;
MR = SCF[SCF_Index[1][Band][0]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = 0.f;
} while (++k < 12);
MR = SCF[SCF_Index[1][Band][1]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = 0.f;
} while (++k < 24);
MR = SCF[SCF_Index[1][Band][2]] * Cc8[Res[1][Band]];
do {
YY [1][k][Band] = QQ [1] [Band] [k] * MR;
YY [0][k][Band] = 0.f;
} while (++k < 36);
}
else { // L==0, R==0
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
}
}
}
LEAVE(162);
return;
}
void
Requantize_MidSideStereo ( Int Stop_Band, const Bool_t* used_MS, unsigned int StreamVersion )
{
if ( StreamVersion & 0x08 )
Requantize_MidSideStereo_SV7F ( Stop_Band, used_MS );
else
Requantize_MidSideStereo_SV7 ( Stop_Band, used_MS );
}
/*
* This is the main requantisation routine for Intensity Stereo.
* It does the same as Requantize_MidSideStereo() but for IS.
*
* Input is:
* - Stop_Band:
* the last band using MS or LR stereo
* - Res[0][Band]:
* Quantisation resolution for every band (0...Stop_Band) and
* the left channel which is used for both channels. Value is 0...17.
* - SCF_Index[3][Band].{L,R}:
* Scale factor for every band (0...Stop_Band), subframe (0...2)
* and channel (L, R).
* - QQ [0] [Band] [36]
* 36 subband samples for every band (0...Stop_Band), both channels use
* the of the left channel
* - SCF[64], Cc7[18], Dc7[18]:
* Lookup tables for Scale factor and Quantisation resolution.
*
* Output is:
* - YY [0]: Left channel subband signals
* - YY [1]: Right channel subband signals
*
* These signals are used for the synthesis filter in the synth*.[ch]
* files to generate the PCM output signal.
*/
void
Requantize_IntensityStereo ( Int Start_Band, Int Stop_Band )
{
Int Band; // Start_Band...Stop_Band
Uint k; // 0...35
Float ML;
Float MR;
ENTER(163);
for ( Band = Start_Band; Band <= Stop_Band; Band++ ) {
if ( Res[0][Band] ) {
k = 0;
ML = SCF[SCF_Index[0][Band][0]] * Cc7[Res[0][Band]] * SS05;
MR = SCF[SCF_Index[1][Band][0]] * Cc7[Res[0][Band]] * SS05;
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 12);
ML = SCF[SCF_Index[0][Band][1]] * Cc7[Res[0][Band]] * SS05;
MR = SCF[SCF_Index[1][Band][1]] * Cc7[Res[0][Band]] * SS05;
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 24);
ML = SCF[SCF_Index[0][Band][2]] * Cc7[Res[0][Band]] * SS05;
MR = SCF[SCF_Index[1][Band][2]] * Cc7[Res[0][Band]] * SS05;
do {
YY [1][k][Band] = QQ [0] [Band] [k] * MR;
YY [0][k][Band] = QQ [0] [Band] [k] * ML;
} while (++k < 36);
}
else {
for ( k = 0; k < 36; k++ ) {
YY [1][k][Band] =
YY [0][k][Band] = 0.f;
}
}
}
LEAVE(163);
return;
}
/*
* Helper function for the qsort() in Resort_HuffTable() to sort a Huffman table
* by its codes.
*/
static int Cdecl
cmp_fn ( const void* p1, const void* p2 )
{
if ( ((const Huffman_t*)p1) -> Code < ((const Huffman_t*)p2) -> Code ) return +1;
if ( ((const Huffman_t*)p1) -> Code > ((const Huffman_t*)p2) -> Code ) return -1;
return 0;
}
/*
* This functions sorts a Huffman table by its codes. It has also two other functions:
*
* - The table contains LSB aligned codes, these are first MSB aligned.
* - The value entry is filled by its position plus 'offset' (Note that
* Make_HuffTable() don't fill this item. Offset can be used to offset
* range for instance from 0...6 to -3...+3.
*
* Note, that this function generates trash if you call it twice!
*/
void
Resort_HuffTable ( Huffman_t* const Table, const size_t elements, Int offset )
{
size_t i;
for ( i = 0; i < elements; i++ ) {
Table[i].Value = i + offset;
Table[i].Code <<= (32 - Table[i].Length);
}
qsort ( Table, elements, sizeof(*Table), cmp_fn );
return;
}
#endif /* MPP_ENCODER */
/*
* Fills out the items Code and Length (but not Value) of a Huffman table
* from a bit packed Huffman table 'src'. Table is not sorted, so this is
* the table which is suitable for an encoder. Be carefully: To get a table
* usable for a decoder you must use Resort_HuffTable() after this
* function. It's a little bit dangerous to divide the functionality, may
* be there is a more secure and handy solution of this problem.
*/
void
Make_HuffTable ( Huffman_t* dst, const HuffSrc_t* src, size_t len )
{
size_t i;
for ( i = 0; i < len; i++,src++,dst++ ) {
dst->Code = src->Code ;
dst->Length = src->Length;
}
}
/*
* Generates a Lookup table for quick Huffman decoding. This table must
* have a size of a power of 2. Input is the pre-sorted Huffman table,
* sorted by Resort_HuffTable() and its length, and the length of the
* lookup table. Output is the Lookup table. It can be used for table based
* decoding (Huffman_decode_fastest) which fully decodes by means of the
* LUT. This is only handy for small huffman codes up to 9...10 bit
* maximum length. For longer codes partial lookup is possible with
* Huffman_decode_faster() which first estimates possibles codes by means
* of LUT and then searches the exact code like the tableless version
* Huffman_decode().
*/
void
Make_LookupTable ( Uint8_t* LUT, size_t LUT_len, const Huffman_t* const Table, const size_t elements )
{
size_t i;
size_t idx = elements;
Uint32_t dval = (Uint32_t)0x80000000L / LUT_len * 2;
Uint32_t val = dval - 1;
for ( i = 0; i < LUT_len; i++, val += dval ) {
while ( idx > 0 && val >= Table[idx-1].Code )
idx--;
*LUT++ = (Uint8_t)idx;
}
return;
}
/* end of tools.c */