Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
BitchX
GitHub Repository: BitchX/BitchX1.3
Path: blob/master/dll/amp/layer2.c
1072 views
1
/* this file is a part of amp software, (C) tomislav uzelac 1996,1997
2
*/
3
4
/* layer2.c MPEG audio layer2 support
5
*
6
* Created by: Tomislav Uzelac Mar 1996
7
* merged with amp, May 19 1997
8
*/
9
#include "amp.h"
10
#include "audio.h"
11
#include "getbits.h"
12
#include "transform.h"
13
14
#define LAYER2
15
#include "layer2.h"
16
17
int layer2_frame(struct AUDIO_HEADER *header,int cnt)
18
{
19
int i,s,sb,ch,gr,bitrate,bound = 0;
20
char (*nbal)[] = &t_nbal0,(*bit_alloc_index)[][16] = &t_alloc0;
21
22
unsigned char allocation[2][32];
23
unsigned char scfsi[2][32];
24
float scalefactor[2][32][3];
25
float subband_sample[2][32][36];
26
int sblimit = 0,nlevels,grouping;
27
28
float c,d;
29
int no_of_bits,mpi;
30
unsigned short sb_sample_buf[3];
31
32
int hsize,fs,mean_frame_size;
33
34
hsize=4;
35
if (header->protection_bit==0) hsize+=2;
36
37
bitrate=t_bitrate[header->ID][3-header->layer][header->bitrate_index];
38
fs=t_sampling_frequency[header->ID][header->sampling_frequency];
39
if (header->ID) mean_frame_size=144000*bitrate/fs;
40
else mean_frame_size=72000*bitrate/fs;
41
42
/* layers 1 and 2 do not have a 'bit reservoir'
43
*/
44
append=data=0;
45
46
fillbfr(mean_frame_size + header->padding_bit - hsize);
47
48
switch (header->mode)
49
{
50
case 0 :
51
case 2 : nch=2; bound=32; bitrate=bitrate/2;
52
break;
53
case 3 : nch=1; bound=32;
54
break;
55
case 1 : nch=2; bitrate=bitrate/2; bound=(header->mode_extension+1)*4;
56
}
57
58
if (header->ID==1) switch (header->sampling_frequency) {
59
case 0 : switch (bitrate) /* 0 = 44.1 kHz */
60
{
61
case 56 :
62
case 64 :
63
case 80 : bit_alloc_index=&t_alloc0;
64
nbal=&t_nbal0;
65
sblimit=27;
66
break;
67
case 96 :
68
case 112 :
69
case 128 :
70
case 160 :
71
case 192 : bit_alloc_index=&t_alloc1;
72
nbal=&t_nbal1;
73
sblimit=30;
74
break;
75
case 32 :
76
case 48 : bit_alloc_index=&t_alloc2;
77
nbal=&t_nbal2;
78
sblimit=8;
79
break;
80
default : /*printf(" bit alloc info no gud ");*/
81
}
82
break;
83
case 1 : switch (bitrate) /* 1 = 48 kHz */
84
{
85
case 56 :
86
case 64 :
87
case 80 :
88
case 96 :
89
case 112 :
90
case 128 :
91
case 160 :
92
case 192 : bit_alloc_index=&t_alloc0;
93
nbal=&t_nbal0;
94
sblimit=27;
95
break;
96
case 32 :
97
case 48 : bit_alloc_index=&t_alloc2;
98
nbal=&t_nbal2;
99
sblimit=8;
100
break;
101
default : /*printf(" bit alloc info no gud ");*/
102
}
103
break;
104
case 2 : switch (bitrate) /* 2 = 32 kHz */
105
{
106
case 56 :
107
case 64 :
108
case 80 : bit_alloc_index=&t_alloc0;
109
nbal=&t_nbal0;
110
sblimit=27;
111
break;
112
case 96 :
113
case 112 :
114
case 128 :
115
case 160 :
116
case 192 : bit_alloc_index=&t_alloc1;
117
nbal=&t_nbal1;
118
sblimit=30;
119
break;
120
case 32 :
121
case 48 : bit_alloc_index=&t_alloc3;
122
nbal=&t_nbal3;
123
sblimit=12;
124
break;
125
default : /*printf("bit alloc info not ok\n");*/
126
}
127
break;
128
default : /*printf("sampling freq. not ok/n");*/
129
} else {
130
bit_alloc_index=&t_allocMPG2;
131
nbal=&t_nbalMPG2;
132
sblimit=30;
133
}
134
135
/*
136
* bit allocation per subband per channel decoding *****************************
137
*/
138
139
if (bound==32) bound=sblimit; /* bound=32 means there is no intensity stereo */
140
141
for (sb=0;sb<bound;sb++)
142
for (ch=0;ch<nch;ch++)
143
allocation[ch][sb]=getbits((*nbal)[sb]);
144
145
for (sb=bound;sb<sblimit;sb++)
146
allocation[1][sb] = allocation[0][sb] = getbits((*nbal)[sb]);
147
148
149
/*
150
* scfsi ***********************************************************************
151
*/
152
153
for (sb=0;sb<sblimit;sb++)
154
for (ch=0;ch<nch;ch++)
155
if (allocation[ch][sb]!=0) scfsi[ch][sb]=getbits(2);
156
else scfsi[ch][sb]=0;
157
158
/*
159
* scalefactors ****************************************************************
160
*/
161
162
for (sb=0;sb<sblimit;sb++)
163
for (ch=0;ch<nch;ch++)
164
if (allocation[ch][sb]!=0) {
165
scalefactor[ch][sb][0]=t_scalefactor[getbits(6)];
166
switch (scfsi[ch][sb])
167
{
168
case 0: scalefactor[ch][sb][1]=t_scalefactor[getbits(6)];
169
scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
170
break;
171
case 1: scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
172
scalefactor[ch][sb][1]=scalefactor[ch][sb][0];
173
break;
174
case 2: scalefactor[ch][sb][1]=scalefactor[ch][sb][0];
175
scalefactor[ch][sb][2]=scalefactor[ch][sb][0];
176
break;
177
case 3: scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
178
scalefactor[ch][sb][1]=scalefactor[ch][sb][2];
179
}
180
}
181
else scalefactor[ch][sb][0]=scalefactor[ch][sb][1]=\
182
scalefactor[ch][sb][2]=0.0;
183
184
185
/*
186
* samples *********************************************************************
187
*/
188
189
for (gr=0;gr<12;gr++) {
190
/*
191
* normal ********************************
192
*/
193
194
for (sb=0;sb<bound;sb++)
195
for (ch=0;ch<nch;ch++)
196
if (allocation[ch][sb]!=0) {
197
mpi=(*bit_alloc_index)[sb][allocation[ch][sb]];
198
no_of_bits=t_bpc[mpi];
199
c=t_c[mpi];
200
d=t_d[mpi];
201
grouping=t_grouping[mpi];
202
nlevels=t_nlevels[mpi];
203
204
if (grouping) {
205
int samplecode=getbits(no_of_bits);
206
convert_samplecode(samplecode,grouping,sb_sample_buf);
207
208
for (s=0;s<3;s++)
209
subband_sample[ch][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[ch][sb][gr/4]);
210
} else {
211
for (s=0;s<3;s++) sb_sample_buf[s]=getbits(no_of_bits);
212
213
for (s=0;s<3;s++) {
214
/*subband_sample[ch][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[ch][sb][gr/4]);*/
215
subband_sample[ch][sb][3*gr+s]=(t_dd[mpi]+sb_sample_buf[s]*t_nli[mpi])*c*scalefactor[ch][sb][gr>>2];
216
}
217
}
218
} else
219
for (s=0;s<3;s++) subband_sample[ch][sb][3*gr+s]=0;
220
221
222
/*
223
* joint stereo ********************************************
224
*/
225
226
for (sb=bound;sb<sblimit;sb++)
227
if (allocation[0][sb]!=0) {
228
/*ispravka!
229
*/
230
mpi=(*bit_alloc_index)[sb][allocation[0][sb]];
231
no_of_bits=t_bpc[mpi];
232
c=t_c[mpi];
233
d=t_d[mpi];
234
grouping=t_grouping[mpi];
235
nlevels=t_nlevels[mpi];
236
237
if (grouping) {
238
int samplecode=getbits(no_of_bits);
239
convert_samplecode(samplecode,grouping,sb_sample_buf);
240
241
for (s=0;s<3;s++) {
242
subband_sample[0][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[0][sb][gr/4]);
243
subband_sample[1][sb][3*gr+s]=subband_sample[0][sb][3*gr+s];
244
}
245
} else {
246
for (s=0;s<3;s++) sb_sample_buf[s]=getbits(no_of_bits);
247
248
for (s=0;s<3;s++) {
249
subband_sample[0][sb][3*gr+s]=subband_sample[1][sb][3*gr+s]=\
250
(t_dd[mpi]+sb_sample_buf[s]*t_nli[mpi])*c*scalefactor[0][sb][gr>>2];
251
}
252
}
253
254
} else for (s=0;s<3;s++) {
255
subband_sample[0][sb][3*gr+s]=0;
256
subband_sample[1][sb][3*gr+s]=0;
257
}
258
259
/*
260
* the rest *******************************************
261
*/
262
for (sb=sblimit;sb<32;sb++)
263
for (ch=0;ch<nch;ch++)
264
265
for (s=0;s<3;s++) subband_sample[ch][sb][3*gr+s]=0;
266
}
267
268
/*
269
* this is, in fact, horrible, but I had to adjust it to amp/mp3. The hack to make downmixing
270
* work is as ugly as possible.
271
*/
272
273
if (A_DOWNMIX && header->mode!=3) {
274
for (ch=0;ch<nch;ch++)
275
for (sb=0;sb<32;sb++)
276
for (i=0;i<36;i++)
277
subband_sample[0][sb][i]=(subband_sample[0][sb][i]+subband_sample[1][sb][i])*0.5f;
278
nch=1;
279
}
280
281
for (ch=0;ch<nch;ch++) {
282
for (sb=0;sb<32;sb++)
283
for (i=0;i<18;i++) res[sb][i]=subband_sample[ch][sb][i];
284
for (i=0;i<18;i++)
285
poly(ch,i);
286
}
287
printout();
288
for (ch=0;ch<nch;ch++) {
289
for (sb=0;sb<32;sb++)
290
for (i=0;i<18;i++) res[sb][i]=subband_sample[ch][sb][i+18];
291
for (i=0;i<18;i++)
292
poly(ch,i);
293
}
294
printout();
295
296
if (A_DOWNMIX && header->mode!=3) nch=2;
297
298
return 0;
299
}
300
/****************************************************************************/
301
/****************************************************************************/
302
303
void convert_samplecode(unsigned int samplecode,unsigned int nlevels,unsigned short* sb_sample_buf)
304
{
305
int i;
306
307
for (i=0;i<3;i++) {
308
*sb_sample_buf=samplecode%nlevels;
309
samplecode=samplecode/nlevels;
310
sb_sample_buf++;
311
}
312
}
313
314
float requantize_sample(unsigned short s4,unsigned short nlevels,float c,float d,float factor)
315
{
316
register float s,s2,s3;
317
s3=-1.0+s4*2.0/(nlevels+1);
318
s2=c*(s3+d);
319
s=factor*s2;
320
return s;
321
}
322
323