/
qdm2.cpp
3221 lines (2742 loc) · 88.1 KB
/
qdm2.cpp
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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
// Based off ffmpeg's QDM2 decoder
#include "common/scummsys.h"
#include "audio/decoders/qdm2.h"
#ifdef AUDIO_QDM2_H
#include "audio/audiostream.h"
#include "audio/decoders/codec.h"
#include "audio/decoders/qdm2data.h"
#include "audio/decoders/raw.h"
#include "common/array.h"
#include "common/debug.h"
#include "common/math.h"
#include "common/stream.h"
#include "common/textconsole.h"
namespace Audio {
enum {
SOFTCLIP_THRESHOLD = 27600,
HARDCLIP_THRESHOLD = 35716,
MPA_MAX_CHANNELS = 2,
MPA_FRAME_SIZE = 1152,
FF_INPUT_BUFFER_PADDING_SIZE = 8
};
typedef int8 sb_int8_array[2][30][64];
/* bit input */
/* buffer, buffer_end and size_in_bits must be present and used by every reader */
struct GetBitContext {
const uint8 *buffer, *bufferEnd;
int index;
int sizeInBits;
};
struct QDM2SubPacket {
int type;
unsigned int size;
const uint8 *data; // pointer to subpacket data (points to input data buffer, it's not a private copy)
};
struct QDM2SubPNode {
QDM2SubPacket *packet;
struct QDM2SubPNode *next; // pointer to next packet in the list, NULL if leaf node
};
struct QDM2Complex {
float re;
float im;
};
struct FFTTone {
float level;
QDM2Complex *complex;
const float *table;
int phase;
int phase_shift;
int duration;
short time_index;
short cutoff;
};
struct FFTCoefficient {
int16 sub_packet;
uint8 channel;
int16 offset;
int16 exp;
uint8 phase;
};
struct VLC {
int32 bits;
int16 (*table)[2]; // code, bits
int32 table_size;
int32 table_allocated;
};
#include "common/pack-start.h"
struct QDM2FFT {
QDM2Complex complex[MPA_MAX_CHANNELS][256];
} PACKED_STRUCT;
#include "common/pack-end.h"
enum RDFTransformType {
RDFT,
IRDFT,
RIDFT,
IRIDFT
};
struct FFTComplex {
float re, im;
};
struct FFTContext {
int nbits;
int inverse;
uint16 *revtab;
FFTComplex *exptab;
FFTComplex *tmpBuf;
int mdctSize; // size of MDCT (i.e. number of input data * 2)
int mdctBits; // n = 2^nbits
// pre/post rotation tables
float *tcos;
float *tsin;
void (*fftPermute)(struct FFTContext *s, FFTComplex *z);
void (*fftCalc)(struct FFTContext *s, FFTComplex *z);
void (*imdctCalc)(struct FFTContext *s, float *output, const float *input);
void (*imdctHalf)(struct FFTContext *s, float *output, const float *input);
void (*mdctCalc)(struct FFTContext *s, float *output, const float *input);
int splitRadix;
int permutation;
};
enum {
FF_MDCT_PERM_NONE = 0,
FF_MDCT_PERM_INTERLEAVE = 1
};
struct RDFTContext {
int nbits;
int inverse;
int signConvention;
// pre/post rotation tables
float *tcos;
float *tsin;
FFTContext fft;
};
class QDM2Stream : public Codec {
public:
QDM2Stream(Common::SeekableReadStream *extraData, DisposeAfterUse::Flag disposeExtraData);
~QDM2Stream();
AudioStream *decodeFrame(Common::SeekableReadStream &stream);
private:
// Parameters from codec header, do not change during playback
uint8 _channels;
uint16 _sampleRate;
uint16 _bitRate;
uint16 _blockSize; // Group
uint16 _frameSize; // FFT
uint16 _packetSize; // Checksum
// Parameters built from header parameters, do not change during playback
int _groupOrder; // order of frame group
int _fftOrder; // order of FFT (actually fft order+1)
int _fftFrameSize; // size of fft frame, in components (1 comples = re + im)
int _sFrameSize; // size of data frame
int _frequencyRange;
int _subSampling; // subsampling: 0=25%, 1=50%, 2=100% */
int _coeffPerSbSelect; // selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2
int _cmTableSelect; // selector for "coding method" tables. Can be 0, 1 (from init: 0-4)
// Packets and packet lists
QDM2SubPacket _subPackets[16]; // the packets themselves
QDM2SubPNode _subPacketListA[16]; // list of all packets
QDM2SubPNode _subPacketListB[16]; // FFT packets B are on list
int _subPacketsB; // number of packets on 'B' list
QDM2SubPNode _subPacketListC[16]; // packets with errors?
QDM2SubPNode _subPacketListD[16]; // DCT packets
// FFT and tones
FFTTone _fftTones[1000];
int _fftToneStart;
int _fftToneEnd;
FFTCoefficient _fftCoefs[1000];
int _fftCoefsIndex;
int _fftCoefsMinIndex[5];
int _fftCoefsMaxIndex[5];
int _fftLevelExp[6];
RDFTContext _rdftCtx;
QDM2FFT _fft;
// I/O data
uint8 *_compressedData;
float _outputBuffer[1024];
// Synthesis filter
int16 ff_mpa_synth_window[512];
int16 _synthBuf[MPA_MAX_CHANNELS][512*2];
int _synthBufOffset[MPA_MAX_CHANNELS];
int32 _sbSamples[MPA_MAX_CHANNELS][128][32];
// Mixed temporary data used in decoding
float _toneLevel[MPA_MAX_CHANNELS][30][64];
int8 _codingMethod[MPA_MAX_CHANNELS][30][64];
int8 _quantizedCoeffs[MPA_MAX_CHANNELS][10][8];
int8 _toneLevelIdxBase[MPA_MAX_CHANNELS][30][8];
int8 _toneLevelIdxHi1[MPA_MAX_CHANNELS][3][8][8];
int8 _toneLevelIdxMid[MPA_MAX_CHANNELS][26][8];
int8 _toneLevelIdxHi2[MPA_MAX_CHANNELS][26];
int8 _toneLevelIdx[MPA_MAX_CHANNELS][30][64];
int8 _toneLevelIdxTemp[MPA_MAX_CHANNELS][30][64];
// Flags
bool _hasErrors; // packet has errors
int _superblocktype_2_3; // select fft tables and some algorithm based on superblock type
int _doSynthFilter; // used to perform or skip synthesis filter
uint8 _subPacket; // 0 to 15
uint32 _superBlockStart;
int _noiseIdx; // index for dithering noise table
byte _emptyBuffer[FF_INPUT_BUFFER_PADDING_SIZE];
VLC _vlcTabLevel;
VLC _vlcTabDiff;
VLC _vlcTabRun;
VLC _fftLevelExpAltVlc;
VLC _fftLevelExpVlc;
VLC _fftStereoExpVlc;
VLC _fftStereoPhaseVlc;
VLC _vlcTabToneLevelIdxHi1;
VLC _vlcTabToneLevelIdxMid;
VLC _vlcTabToneLevelIdxHi2;
VLC _vlcTabType30;
VLC _vlcTabType34;
VLC _vlcTabFftToneOffset[5];
bool _vlcsInitialized;
void initVlc(void);
uint16 _softclipTable[HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD + 1];
void softclipTableInit(void);
float _noiseTable[4096];
byte _randomDequantIndex[256][5];
byte _randomDequantType24[128][3];
void rndTableInit(void);
float _noiseSamples[128];
void initNoiseSamples(void);
void average_quantized_coeffs(void);
void build_sb_samples_from_noise(int sb);
void fix_coding_method_array(int sb, int channels, sb_int8_array coding_method);
void fill_tone_level_array(int flag);
void fill_coding_method_array(sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp,
sb_int8_array coding_method, int nb_channels,
int c, int superblocktype_2_3, int cm_table_select);
void synthfilt_build_sb_samples(GetBitContext *gb, int length, int sb_min, int sb_max);
void init_quantized_coeffs_elem0(int8 *quantized_coeffs, GetBitContext *gb, int length);
void init_tone_level_dequantization(GetBitContext *gb, int length);
void process_subpacket_9(QDM2SubPNode *node);
void process_subpacket_10(QDM2SubPNode *node, int length);
void process_subpacket_11(QDM2SubPNode *node, int length);
void process_subpacket_12(QDM2SubPNode *node, int length);
void process_synthesis_subpackets(QDM2SubPNode *list);
void qdm2_decode_super_block(void);
void qdm2_fft_init_coefficient(int sub_packet, int offset, int duration,
int channel, int exp, int phase);
void qdm2_fft_decode_tones(int duration, GetBitContext *gb, int b);
void qdm2_decode_fft_packets(void);
void qdm2_fft_generate_tone(FFTTone *tone);
void qdm2_fft_tone_synthesizer(uint8 sub_packet);
void qdm2_calculate_fft(int channel);
void qdm2_synthesis_filter(uint8 index);
bool qdm2_decodeFrame(Common::SeekableReadStream &in, QueuingAudioStream *audioStream);
};
// Fix compilation for non C99-compliant compilers, like MSVC
#ifndef int64_t
typedef signed long long int int64_t;
#endif
#define QDM2_LIST_ADD(list, size, packet) \
do { \
if (size > 0) \
list[size - 1].next = &list[size]; \
list[size].packet = packet; \
list[size].next = NULL; \
size++; \
} while(0)
// Result is 8, 16 or 30
#define QDM2_SB_USED(subSampling) (((subSampling) >= 2) ? 30 : 8 << (subSampling))
#define FIX_NOISE_IDX(noiseIdx) \
if ((noiseIdx) >= 3840) \
(noiseIdx) -= 3840 \
#define SB_DITHERING_NOISE(sb, noiseIdx) (_noiseTable[(noiseIdx)++] * sb_noise_attenuation[(sb)])
static inline void initGetBits(GetBitContext *s, const uint8 *buffer, int bitSize) {
int bufferSize = (bitSize + 7) >> 3;
if (bufferSize < 0 || bitSize < 0) {
bufferSize = bitSize = 0;
buffer = NULL;
}
s->buffer = buffer;
s->sizeInBits = bitSize;
s->bufferEnd = buffer + bufferSize;
s->index = 0;
}
static inline int getBitsCount(GetBitContext *s) {
return s->index;
}
static inline unsigned int getBits1(GetBitContext *s) {
int index;
uint8 result;
index = s->index;
result = s->buffer[index >> 3];
result >>= (index & 0x07);
result &= 1;
index++;
s->index = index;
return result;
}
static inline unsigned int getBits(GetBitContext *s, int n) {
int tmp, reCache, reIndex;
reIndex = s->index;
reCache = READ_LE_UINT32((const uint8 *)s->buffer + (reIndex >> 3)) >> (reIndex & 0x07);
tmp = (reCache) & ((uint32)0xffffffff >> (32 - n));
s->index = reIndex + n;
return tmp;
}
static inline void skipBits(GetBitContext *s, int n) {
s->index += n;
}
#define BITS_LEFT(length, gb) ((length) - getBitsCount((gb)))
static int splitRadixPermutation(int i, int n, int inverse) {
if (n <= 2)
return i & 1;
int m = n >> 1;
if(!(i & m))
return splitRadixPermutation(i, m, inverse) * 2;
m >>= 1;
if (inverse == !(i & m))
return splitRadixPermutation(i, m, inverse) * 4 + 1;
return splitRadixPermutation(i, m, inverse) * 4 - 1;
}
// sin(2*pi*x/n) for 0<=x<n/4, followed by n/2<=x<3n/4
float ff_sin_16[8];
float ff_sin_32[16];
float ff_sin_64[32];
float ff_sin_128[64];
float ff_sin_256[128];
float ff_sin_512[256];
float ff_sin_1024[512];
float ff_sin_2048[1024];
float ff_sin_4096[2048];
float ff_sin_8192[4096];
float ff_sin_16384[8192];
float ff_sin_32768[16384];
float ff_sin_65536[32768];
float *ff_sin_tabs[] = {
NULL, NULL, NULL, NULL,
ff_sin_16, ff_sin_32, ff_sin_64, ff_sin_128, ff_sin_256, ff_sin_512, ff_sin_1024,
ff_sin_2048, ff_sin_4096, ff_sin_8192, ff_sin_16384, ff_sin_32768, ff_sin_65536,
};
// cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse
float ff_cos_16[8];
float ff_cos_32[16];
float ff_cos_64[32];
float ff_cos_128[64];
float ff_cos_256[128];
float ff_cos_512[256];
float ff_cos_1024[512];
float ff_cos_2048[1024];
float ff_cos_4096[2048];
float ff_cos_8192[4096];
float ff_cos_16384[8192];
float ff_cos_32768[16384];
float ff_cos_65536[32768];
float *ff_cos_tabs[] = {
NULL, NULL, NULL, NULL,
ff_cos_16, ff_cos_32, ff_cos_64, ff_cos_128, ff_cos_256, ff_cos_512, ff_cos_1024,
ff_cos_2048, ff_cos_4096, ff_cos_8192, ff_cos_16384, ff_cos_32768, ff_cos_65536,
};
void initCosineTables(int index) {
int m = 1 << index;
double freq = 2 * M_PI / m;
float *tab = ff_cos_tabs[index];
for (int i = 0; i <= m / 4; i++)
tab[i] = cos(i * freq);
for (int i = 1; i < m / 4; i++)
tab[m / 2 - i] = tab[i];
}
void fftPermute(FFTContext *s, FFTComplex *z) {
const uint16 *revtab = s->revtab;
int np = 1 << s->nbits;
if (s->tmpBuf) {
// TODO: handle split-radix permute in a more optimal way, probably in-place
for (int j = 0; j < np; j++)
s->tmpBuf[revtab[j]] = z[j];
memcpy(z, s->tmpBuf, np * sizeof(FFTComplex));
return;
}
// reverse
for (int j = 0; j < np; j++) {
int k = revtab[j];
if (k < j) {
FFTComplex tmp = z[k];
z[k] = z[j];
z[j] = tmp;
}
}
}
#define DECL_FFT(n,n2,n4) \
static void fft##n(FFTComplex *z) { \
fft##n2(z); \
fft##n4(z + n4 * 2); \
fft##n4(z + n4 * 3); \
pass(z, ff_cos_##n, n4 / 2); \
}
#ifndef M_SQRT1_2
#define M_SQRT1_2 7.0710678118654752440E-1
#endif
#define sqrthalf (float)M_SQRT1_2
#define BF(x,y,a,b) { \
x = a - b; \
y = a + b; \
}
#define BUTTERFLIES(a0, a1, a2, a3) { \
BF(t3, t5, t5, t1); \
BF(a2.re, a0.re, a0.re, t5); \
BF(a3.im, a1.im, a1.im, t3); \
BF(t4, t6, t2, t6); \
BF(a3.re, a1.re, a1.re, t4); \
BF(a2.im, a0.im, a0.im, t6); \
}
// force loading all the inputs before storing any.
// this is slightly slower for small data, but avoids store->load aliasing
// for addresses separated by large powers of 2.
#define BUTTERFLIES_BIG(a0, a1, a2, a3) { \
float r0 = a0.re, i0 = a0.im, r1 = a1.re, i1 = a1.im; \
BF(t3, t5, t5, t1); \
BF(a2.re, a0.re, r0, t5); \
BF(a3.im, a1.im, i1, t3); \
BF(t4, t6, t2, t6); \
BF(a3.re, a1.re, r1, t4); \
BF(a2.im, a0.im, i0, t6); \
}
#define TRANSFORM(a0, a1, a2, a3, wre, wim) { \
t1 = a2.re * wre + a2.im * wim; \
t2 = a2.im * wre - a2.re * wim; \
t5 = a3.re * wre - a3.im * wim; \
t6 = a3.im * wre + a3.re * wim; \
BUTTERFLIES(a0, a1, a2, a3) \
}
#define TRANSFORM_ZERO(a0, a1, a2, a3) { \
t1 = a2.re; \
t2 = a2.im; \
t5 = a3.re; \
t6 = a3.im; \
BUTTERFLIES(a0, a1, a2, a3) \
}
// z[0...8n-1], w[1...2n-1]
#define PASS(name) \
static void name(FFTComplex *z, const float *wre, unsigned int n) { \
float t1, t2, t3, t4, t5, t6; \
int o1 = 2 * n; \
int o2 = 4 * n; \
int o3 = 6 * n; \
const float *wim = wre + o1; \
n--; \
\
TRANSFORM_ZERO(z[0], z[o1], z[o2], z[o3]); \
TRANSFORM(z[1], z[o1 + 1], z[o2 + 1], z[o3 + 1], wre[1], wim[-1]); \
\
do { \
z += 2; \
wre += 2; \
wim -= 2; \
TRANSFORM(z[0], z[o1], z[o2], z[o3], wre[0], wim[0]); \
TRANSFORM(z[1], z[o1 + 1],z[o2 + 1], z[o3 + 1], wre[1], wim[-1]); \
} while(--n); \
}
PASS(pass)
#undef BUTTERFLIES
#define BUTTERFLIES BUTTERFLIES_BIG
PASS(pass_big)
static void fft4(FFTComplex *z) {
float t1, t2, t3, t4, t5, t6, t7, t8;
BF(t3, t1, z[0].re, z[1].re);
BF(t8, t6, z[3].re, z[2].re);
BF(z[2].re, z[0].re, t1, t6);
BF(t4, t2, z[0].im, z[1].im);
BF(t7, t5, z[2].im, z[3].im);
BF(z[3].im, z[1].im, t4, t8);
BF(z[3].re, z[1].re, t3, t7);
BF(z[2].im, z[0].im, t2, t5);
}
static void fft8(FFTComplex *z) {
float t1, t2, t3, t4, t5, t6, t7, t8;
fft4(z);
BF(t1, z[5].re, z[4].re, -z[5].re);
BF(t2, z[5].im, z[4].im, -z[5].im);
BF(t3, z[7].re, z[6].re, -z[7].re);
BF(t4, z[7].im, z[6].im, -z[7].im);
BF(t8, t1, t3, t1);
BF(t7, t2, t2, t4);
BF(z[4].re, z[0].re, z[0].re, t1);
BF(z[4].im, z[0].im, z[0].im, t2);
BF(z[6].re, z[2].re, z[2].re, t7);
BF(z[6].im, z[2].im, z[2].im, t8);
TRANSFORM(z[1], z[3], z[5], z[7], sqrthalf, sqrthalf);
}
#undef BF
DECL_FFT(16,8,4)
DECL_FFT(32,16,8)
DECL_FFT(64,32,16)
DECL_FFT(128,64,32)
DECL_FFT(256,128,64)
DECL_FFT(512,256,128)
#define pass pass_big
DECL_FFT(1024,512,256)
DECL_FFT(2048,1024,512)
DECL_FFT(4096,2048,1024)
DECL_FFT(8192,4096,2048)
DECL_FFT(16384,8192,4096)
DECL_FFT(32768,16384,8192)
DECL_FFT(65536,32768,16384)
void fftCalc(FFTContext *s, FFTComplex *z) {
static void (* const fftDispatch[])(FFTComplex *) = {
fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
fft2048, fft4096, fft8192, fft16384, fft32768, fft65536,
};
fftDispatch[s->nbits - 2](z);
}
// complex multiplication: p = a * b
#define CMUL(pre, pim, are, aim, bre, bim) \
{\
float _are = (are); \
float _aim = (aim); \
float _bre = (bre); \
float _bim = (bim); \
(pre) = _are * _bre - _aim * _bim; \
(pim) = _are * _bim + _aim * _bre; \
}
/**
* Compute the middle half of the inverse MDCT of size N = 2^nbits,
* thus excluding the parts that can be derived by symmetry
* @param output N/2 samples
* @param input N/2 samples
*/
void imdctHalfC(FFTContext *s, float *output, const float *input) {
const uint16 *revtab = s->revtab;
const float *tcos = s->tcos;
const float *tsin = s->tsin;
FFTComplex *z = (FFTComplex *)output;
int n = 1 << s->mdctBits;
int n2 = n >> 1;
int n4 = n >> 2;
int n8 = n >> 3;
// pre rotation
const float *in1 = input;
const float *in2 = input + n2 - 1;
for (int k = 0; k < n4; k++) {
int j = revtab[k];
CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
in1 += 2;
in2 -= 2;
}
fftCalc(s, z);
// post rotation + reordering
for (int k = 0; k < n8; k++) {
float r0, i0, r1, i1;
CMUL(r0, i1, z[n8 - k - 1].im, z[n8 - k - 1].re, tsin[n8 - k - 1], tcos[n8 - k - 1]);
CMUL(r1, i0, z[n8 + k].im, z[n8 + k].re, tsin[n8 + k], tcos[n8 + k]);
z[n8 - k - 1].re = r0;
z[n8 - k - 1].im = i0;
z[n8 + k].re = r1;
z[n8 + k].im = i1;
}
}
/**
* Compute inverse MDCT of size N = 2^nbits
* @param output N samples
* @param input N/2 samples
*/
void imdctCalcC(FFTContext *s, float *output, const float *input) {
int n = 1 << s->mdctBits;
int n2 = n >> 1;
int n4 = n >> 2;
imdctHalfC(s, output + n4, input);
for (int k = 0; k < n4; k++) {
output[k] = -output[n2 - k - 1];
output[n - k - 1] = output[n2 + k];
}
}
/**
* Compute MDCT of size N = 2^nbits
* @param input N samples
* @param out N/2 samples
*/
void mdctCalcC(FFTContext *s, float *out, const float *input) {
const uint16 *revtab = s->revtab;
const float *tcos = s->tcos;
const float *tsin = s->tsin;
FFTComplex *x = (FFTComplex *)out;
int n = 1 << s->mdctBits;
int n2 = n >> 1;
int n4 = n >> 2;
int n8 = n >> 3;
int n3 = 3 * n4;
// pre rotation
for (int i = 0; i < n8; i++) {
float re = -input[2 * i + 3 * n4] - input[n3 - 1 - 2 * i];
float im = -input[n4 + 2 * i] + input[n4 - 1 - 2 * i];
int j = revtab[i];
CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
re = input[2 * i] - input[n2 - 1 - 2 * i];
im = -(input[n2 + 2 * i] + input[n - 1 - 2 * i]);
j = revtab[n8 + i];
CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
}
fftCalc(s, x);
// post rotation
for (int i = 0; i < n8; i++) {
float r0, i0, r1, i1;
CMUL(i1, r0, x[n8 - i - 1].re, x[n8 - i - 1].im, -tsin[n8 - i - 1], -tcos[n8 - i - 1]);
CMUL(i0, r1, x[n8 + i].re, x[n8 + i].im, -tsin[n8 + i], -tcos[n8 + i]);
x[n8 - i - 1].re = r0;
x[n8 - i - 1].im = i0;
x[n8 + i].re = r1;
x[n8 + i].im = i1;
}
}
int fftInit(FFTContext *s, int nbits, int inverse) {
int i, j, m, n;
float alpha, c1, s1, s2;
if (nbits < 2 || nbits > 16)
goto fail;
s->nbits = nbits;
n = 1 << nbits;
s->tmpBuf = NULL;
s->exptab = (FFTComplex *)malloc((n / 2) * sizeof(FFTComplex));
if (!s->exptab)
goto fail;
s->revtab = (uint16 *)malloc(n * sizeof(uint16));
if (!s->revtab)
goto fail;
s->inverse = inverse;
s2 = inverse ? 1.0 : -1.0;
s->fftPermute = fftPermute;
s->fftCalc = fftCalc;
s->imdctCalc = imdctCalcC;
s->imdctHalf = imdctHalfC;
s->mdctCalc = mdctCalcC;
s->splitRadix = 1;
if (s->splitRadix) {
for (j = 4; j <= nbits; j++)
initCosineTables(j);
for (i = 0; i < n; i++)
s->revtab[-splitRadixPermutation(i, n, s->inverse) & (n - 1)] = i;
s->tmpBuf = (FFTComplex *)malloc(n * sizeof(FFTComplex));
} else {
for (i = 0; i < n / 2; i++) {
alpha = 2 * M_PI * (float)i / (float)n;
c1 = cos(alpha);
s1 = sin(alpha) * s2;
s->exptab[i].re = c1;
s->exptab[i].im = s1;
}
//int np = 1 << nbits;
//int nblocks = np >> 3;
//int np2 = np >> 1;
// compute bit reverse table
for (i = 0; i < n; i++) {
m = 0;
for (j = 0; j < nbits; j++)
m |= ((i >> j) & 1) << (nbits - j - 1);
s->revtab[i] = m;
}
}
return 0;
fail:
free(&s->revtab);
free(&s->exptab);
free(&s->tmpBuf);
return -1;
}
/**
* Sets up a real FFT.
* @param nbits log2 of the length of the input array
* @param trans the type of transform
*/
int rdftInit(RDFTContext *s, int nbits, RDFTransformType trans) {
int n = 1 << nbits;
const double theta = (trans == RDFT || trans == IRIDFT ? -1 : 1) * 2 * M_PI / n;
s->nbits = nbits;
s->inverse = trans == IRDFT || trans == IRIDFT;
s->signConvention = trans == RIDFT || trans == IRIDFT ? 1 : -1;
if (nbits < 4 || nbits > 16)
return -1;
if (fftInit(&s->fft, nbits - 1, trans == IRDFT || trans == RIDFT) < 0)
return -1;
initCosineTables(nbits);
s->tcos = ff_cos_tabs[nbits];
s->tsin = ff_sin_tabs[nbits] + (trans == RDFT || trans == IRIDFT) * (n >> 2);
for (int i = 0; i < n >> 2; i++)
s->tsin[i] = sin(i*theta);
return 0;
}
/** Map one real FFT into two parallel real even and odd FFTs. Then interleave
* the two real FFTs into one complex FFT. Unmangle the results.
* ref: http://www.engineeringproductivitytools.com/stuff/T0001/PT10.HTM
*/
void rdftCalc(RDFTContext *s, float *data) {
FFTComplex ev, od;
const int n = 1 << s->nbits;
const float k1 = 0.5;
const float k2 = 0.5 - s->inverse;
const float *tcos = s->tcos;
const float *tsin = s->tsin;
if (!s->inverse) {
fftPermute(&s->fft, (FFTComplex *)data);
fftCalc(&s->fft, (FFTComplex *)data);
}
// i=0 is a special case because of packing, the DC term is real, so we
// are going to throw the N/2 term (also real) in with it.
ev.re = data[0];
data[0] = ev.re + data[1];
data[1] = ev.re - data[1];
int i;
for (i = 1; i < n >> 2; i++) {
int i1 = i * 2;
int i2 = n - i1;
// Separate even and odd FFTs
ev.re = k1 * (data[i1] + data[i2]);
od.im = -k2 * (data[i1] - data[i2]);
ev.im = k1 * (data[i1 + 1] - data[i2 + 1]);
od.re = k2 * (data[i1 + 1] + data[i2 + 1]);
// Apply twiddle factors to the odd FFT and add to the even FFT
data[i1] = ev.re + od.re * tcos[i] - od.im * tsin[i];
data[i1 + 1] = ev.im + od.im * tcos[i] + od.re * tsin[i];
data[i2] = ev.re - od.re * tcos[i] + od.im * tsin[i];
data[i2 + 1] = -ev.im + od.im * tcos[i] + od.re * tsin[i];
}
data[i * 2 + 1] = s->signConvention * data[i * 2 + 1];
if (s->inverse) {
data[0] *= k1;
data[1] *= k1;
fftPermute(&s->fft, (FFTComplex *)data);
fftCalc(&s->fft, (FFTComplex *)data);
}
}
// half mpeg encoding window (full precision)
const int32 ff_mpa_enwindow[257] = {
0, -1, -1, -1, -1, -1, -1, -2,
-2, -2, -2, -3, -3, -4, -4, -5,
-5, -6, -7, -7, -8, -9, -10, -11,
-13, -14, -16, -17, -19, -21, -24, -26,
-29, -31, -35, -38, -41, -45, -49, -53,
-58, -63, -68, -73, -79, -85, -91, -97,
-104, -111, -117, -125, -132, -139, -147, -154,
-161, -169, -176, -183, -190, -196, -202, -208,
213, 218, 222, 225, 227, 228, 228, 227,
224, 221, 215, 208, 200, 189, 177, 163,
146, 127, 106, 83, 57, 29, -2, -36,
-72, -111, -153, -197, -244, -294, -347, -401,
-459, -519, -581, -645, -711, -779, -848, -919,
-991, -1064, -1137, -1210, -1283, -1356, -1428, -1498,
-1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962,
-2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063,
2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535,
1414, 1280, 1131, 970, 794, 605, 402, 185,
-45, -288, -545, -814, -1095, -1388, -1692, -2006,
-2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788,
-5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597,
-7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585,
-9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750,
-9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134,
6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082,
70, -998, -2122, -3300, -4533, -5818, -7154, -8540,
-9975,-11455,-12980,-14548,-16155,-17799,-19478,-21189,
-22929,-24694,-26482,-28289,-30112,-31947,-33791,-35640,
-37489,-39336,-41176,-43006,-44821,-46617,-48390,-50137,
-51853,-53534,-55178,-56778,-58333,-59838,-61289,-62684,
-64019,-65290,-66494,-67629,-68692,-69679,-70590,-71420,
-72169,-72835,-73415,-73908,-74313,-74630,-74856,-74992,
75038
};
void ff_mpa_synth_init(int16 *window) {
int i;
int32 v;
// max = 18760, max sum over all 16 coefs : 44736
for(i = 0; i < 257; i++) {
v = ff_mpa_enwindow[i];
v = (v + 2) >> 2;
window[i] = v;
if ((i & 63) != 0)
v = -v;
if (i != 0)
window[512 - i] = v;
}
}
static inline uint16 round_sample(int *sum) {
int sum1;
sum1 = (*sum) >> 14;
*sum &= (1 << 14)-1;
if (sum1 < (-0x7fff - 1))
sum1 = (-0x7fff - 1);
if (sum1 > 0x7fff)
sum1 = 0x7fff;
return sum1;
}
static inline int MULH(int a, int b) {
return ((int64_t)(a) * (int64_t)(b))>>32;
}
// signed 16x16 -> 32 multiply add accumulate
#define MACS(rt, ra, rb) rt += (ra) * (rb)
#define MLSS(rt, ra, rb) ((rt) -= (ra) * (rb))
#define SUM8(op, sum, w, p)\
{\
op(sum, (w)[0 * 64], (p)[0 * 64]);\
op(sum, (w)[1 * 64], (p)[1 * 64]);\
op(sum, (w)[2 * 64], (p)[2 * 64]);\
op(sum, (w)[3 * 64], (p)[3 * 64]);\
op(sum, (w)[4 * 64], (p)[4 * 64]);\
op(sum, (w)[5 * 64], (p)[5 * 64]);\
op(sum, (w)[6 * 64], (p)[6 * 64]);\
op(sum, (w)[7 * 64], (p)[7 * 64]);\
}
#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \
{\
tmp_s = p[0 * 64];\
op1(sum1, (w1)[0 * 64], tmp_s);\
op2(sum2, (w2)[0 * 64], tmp_s);\
tmp_s = p[1 * 64];\
op1(sum1, (w1)[1 * 64], tmp_s);\
op2(sum2, (w2)[1 * 64], tmp_s);\
tmp_s = p[2 * 64];\
op1(sum1, (w1)[2 * 64], tmp_s);\
op2(sum2, (w2)[2 * 64], tmp_s);\
tmp_s = p[3 * 64];\
op1(sum1, (w1)[3 * 64], tmp_s);\
op2(sum2, (w2)[3 * 64], tmp_s);\
tmp_s = p[4 * 64];\
op1(sum1, (w1)[4 * 64], tmp_s);\
op2(sum2, (w2)[4 * 64], tmp_s);\
tmp_s = p[5 * 64];\
op1(sum1, (w1)[5 * 64], tmp_s);\
op2(sum2, (w2)[5 * 64], tmp_s);\
tmp_s = p[6 * 64];\
op1(sum1, (w1)[6 * 64], tmp_s);\
op2(sum2, (w2)[6 * 64], tmp_s);\
tmp_s = p[7 * 64];\
op1(sum1, (w1)[7 * 64], tmp_s);\
op2(sum2, (w2)[7 * 64], tmp_s);\
}
#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5))
// tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j)))
// cos(i*pi/64)
#define COS0_0 FIXHR(0.50060299823519630134/2)
#define COS0_1 FIXHR(0.50547095989754365998/2)
#define COS0_2 FIXHR(0.51544730992262454697/2)
#define COS0_3 FIXHR(0.53104259108978417447/2)
#define COS0_4 FIXHR(0.55310389603444452782/2)
#define COS0_5 FIXHR(0.58293496820613387367/2)
#define COS0_6 FIXHR(0.62250412303566481615/2)
#define COS0_7 FIXHR(0.67480834145500574602/2)
#define COS0_8 FIXHR(0.74453627100229844977/2)
#define COS0_9 FIXHR(0.83934964541552703873/2)
#define COS0_10 FIXHR(0.97256823786196069369/2)
#define COS0_11 FIXHR(1.16943993343288495515/4)
#define COS0_12 FIXHR(1.48416461631416627724/4)
#define COS0_13 FIXHR(2.05778100995341155085/8)
#define COS0_14 FIXHR(3.40760841846871878570/8)